相关疑难解决方法(0)

249
推荐指数
7
解决办法
7万
查看次数

使用POD高效移动课程

如何有效地移动具有大量POD成员的班级?例:

struct{
    int a1;
    int a2;
    int a3;
    ...
    ...
    ...
};
Run Code Online (Sandbox Code Playgroud)

通过'move',我的意思是行为类似于移动语义(std :: move).

c++ c++11

6
推荐指数
3
解决办法
2993
查看次数

如何在 std::function 中捕获 unique_ptr

我需要将 a 移至关闭unique_ptr状态std::function。我在 C++14 中使用通用 lambda 捕获。

auto ptr = make_unique<Foo>();

// Works.
auto lambda = [p = move(ptr)] { };

// This does not compile.
std::function<void()> func = [p = move(ptr)] { };
Run Code Online (Sandbox Code Playgroud)

它试图将 lambda 捕获复制而不是移动到std::function. 相关错误是:

 copy constructor of '' is implicitly deleted because field '' has a deleted copy
      constructor
  std::function<void()> func = [p = move(ptr)] { };
Run Code Online (Sandbox Code Playgroud)

这里的例子会让这看起来可行。

请注意,这里的答案只是重复 isocpp.org 上的示例。

我可以移动到 ashared_ptr如下:

shared_ptr<Foo> …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

5
推荐指数
1
解决办法
1644
查看次数

在编程的上下文中,“拥有”是什么意思?

cppreference使用它来描述 std::string_view:

devtutsodocumentation 也 使用它来描述 std::string_view :

C++17 引入了std::string_view,它只是一个非拥有范围的 const chars,可实现为一对指针或一个指针和一个长度。

和这里的各种其他问题和答案参考它,但我找不到它的含义的任何解释。

c++ string-view

5
推荐指数
1
解决办法
89
查看次数

C++ 右值shared_ptr 和右值weak_ptr

std::shared_ptr<std::string> test() {
  return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
cout << *test() << endl;
Run Code Online (Sandbox Code Playgroud)

上面的代码有效。有人可以告诉我“sdsd”字符串存储在哪里吗?我原以为它会出错,因为右值是一个临时对象。右值复制并存储在哪里?

使用weak_ptr

std::weak_ptr<std::string> test() {
  return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
  cout << *test().lock() << endl;
Run Code Online (Sandbox Code Playgroud)

有趣的是上面的代码出错了。有什么不同?

c++ weak-references shared-ptr

5
推荐指数
1
解决办法
167
查看次数

关于operator new重载和异常的问题

为什么这段代码会输出很多“here”?

我认为程序应该在throw std::invalid_argument( "fool" );调用 after 时终止。

#include <memory>
#include <iostream>

void* operator new(std::size_t size)
{
    std::cout << "here" << std::endl;
    throw std::invalid_argument( "fool" );   //commit out this, there would be many many ouputs
    return std::malloc(size);
}

void operator delete(void* ptr)
{
    return free(ptr);
}


int main()
{
    //std::unique_ptr<int> point2int(new int(999));

    int* rawpoint2int = new int(666);
}
Run Code Online (Sandbox Code Playgroud)

c++ exception new-operator c++11

3
推荐指数
1
解决办法
153
查看次数

std::vector::~vector 的时间复杂度是多少?

所以我在争论是否在我正在制作的程序中使用动态数组Node*std::vector<Node*>数组。该程序本质上是在链表(定制的)上测试 STL 排序,因此将 LL 中的每个 nodeptr 插入到一个数组(动态分配的数组或向量)中,然后对其调用 std::sort。因此,在对数组进行排序后移动 LL 的指针时,我最初使用一个向量来保存所有节点。但是我意识到一旦超出范围,向量就会通过 std::vector::~vector 被销毁。根据 cppreference,我认为这是 O(n) 时间:

std::vector::~vector() 析构向量。调用元素的析构函数并释放已使用的存储空间。请注意,如果元素是指针,则指向的对象不会被销毁。

我相信粗体部分表示 O(n) 时间。所以我想知道你是否可以分配一个动态数组来保存节点,然后在所有排序后删除该数组。(而使用 ~vector(),您将遍历列表两次;一次在排序期间,第二次在释放数组时)因为如果是这样,那么您将从代码中减去整个 O(n) 操作,如果可能的话,这会更快。

我在这里误解了,还是因此常规动态数组会更快?

c++ vector dynamic-arrays

2
推荐指数
1
解决办法
104
查看次数

传递给函数后如何使用unique_ptr?

我刚刚开始学习新的C ++内存模型:

#include <string>
#include <iostream>
#include <memory>

void print(unique_ptr<std::string> s) {
        std::cout << *s << " " <<  s->size() << "\n";
}

int main() {
        auto s = std::make_unique<std::string>("Hello");
        print(std::move(s));
        std::cout << *s;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,调用应该会cout << *s;导致段错误。我知道为什么会这样。但是我也想知道是否有办法找回所有权。我希望能够在将值传递给函数后使用它。

c++ unique-ptr

1
推荐指数
1
解决办法
62
查看次数

使用 make_shared&lt;U[]&gt;( std::size_t N )` 时出错

我正在尝试实现一个固定大小的多维数组,其大小在运行时确定。make_shared与( )的 (2) 重载template<class T> shared_ptr<T> make_shared(std::size_t N) // T is U[]。但是,我面临编译错误(日志如下)。shared如果我将s 更改为其对应项,则不会出现该错误unique。我的问题是,

\n
    \n
  • 这个错误是关于什么的?
  • \n
  • 为什么unique有效?
  • \n
  • 有更好的方法来实现这种运行时固定的多维数组容器吗?
  • \n
\n

最小工作示例:

\n
#include <memory>\n#include <iostream>\nint main() {\n    typedef int cell_t;\n    std::size_t x, y;\n    std::cin >> y >> x;\n    auto layout = std::make_shared<std::shared_ptr<cell_t[]>[]>(y);\n    for (std::size_t i = 0; i < y; i += 1) {\n        layout[i] = std::make_shared<cell_t[]>(x);\n    }\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

错误消息如下g++-10(为简洁起见,省略了注释)

\n
In file included …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers shared-ptr c++20

1
推荐指数
1
解决办法
1544
查看次数

为什么通过指定错误来连接两个链表?

所以我们的讲师只是向我们展示了如何连接两个链接列表然后继续并向我们展示了如果实现了将会出错的示例,我似乎并不完全理解为什么它不能正常工作,因为它不是解释.如果我以这种方式连接两个链表,会出现什么问题:

template <typename T>
void LList<T>::concat(LList<T> const & list) {
    end->next = list.start;
    end = list.end;
}
Run Code Online (Sandbox Code Playgroud)

c++ linked-list

0
推荐指数
1
解决办法
83
查看次数

从堆栈分配的原始指针构造智能指针

有人可以告诉我这里发生了什么吗?

int* stackint = new int(5);

{
    std::unique_ptr<int> myInt(stackint);    
    *myInt = 8;

}

std::cout << *stackint; // 0
Run Code Online (Sandbox Code Playgroud)

这里到底发生了什么?我理解智能指针,当你用 new 或 make_unique 构造它们时,当你将堆栈指针传递给它们的构造函数时会发生什么?

c++ smart-pointers

0
推荐指数
1
解决办法
519
查看次数

如何避免在 C++ 中使用 new 运算符?

我有一个 C++ 程序,可以为文件中的所有字符创建霍夫曼代码。它运行良好,但我想在不使用 new 运算符的情况下创建节点,因为我知道您不应该使用它。我尝试使用向量全局变量来保存节点,但这不起作用。

std::vector<Node> nodes;

Node* create_node(unsigned char value, unsigned long long counter, Node* left, Node* right) {

    Node temp;
    temp.m_value = value;
    temp.m_counter = counter;
    temp.m_left = left;
    temp.m_right = right;

    nodes.push_back(temp);
    return &nodes[nodes.size() - 1];
}
Run Code Online (Sandbox Code Playgroud)

编辑:我添加了更多代码,我没有真正解释什么不起作用。问题在于generate_code(),它永远不会到达 nullptr。我也尝试使用 Node 而不是 Node* 但同样的事情发生了。

void generate_code(Node* current, std::string code, std::map<unsigned char, std::string>& char_codes) {

    if (current == nullptr) {
        return;
    }

    if (!current->m_left && !current->m_right) {
        char_codes[current->m_value] = code;
    }

    generate_code(current->m_left, code + "0", char_codes);
    generate_code(current->m_right, …
Run Code Online (Sandbox Code Playgroud)

c++ new-operator

0
推荐指数
1
解决办法
155
查看次数