如何有效地移动具有大量POD成员的班级?例:
struct{
int a1;
int a2;
int a3;
...
...
...
};
Run Code Online (Sandbox Code Playgroud)
通过'move',我的意思是行为类似于移动语义(std :: move).
我需要将 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) cppreference使用它来描述 std::string_view:
std::basic_string_view
(C++17) -对字符串子序列的轻量级非拥有只读视图。
devtut 和 sodocumentation 也 使用它来描述 std::string_view :
C++17 引入了
std::string_view
,它只是一个非拥有范围的const char
s,可实现为一对指针或一个指针和一个长度。
和这里的各种其他问题和答案参考它,但我找不到它的含义的任何解释。
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)
有趣的是上面的代码出错了。有什么不同?
为什么这段代码会输出很多“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) 所以我在争论是否在我正在制作的程序中使用动态数组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 ++内存模型:
#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;
导致段错误。我知道为什么会这样。但是我也想知道是否有办法找回所有权。我希望能够在将值传递给函数后使用它。
我正在尝试实现一个固定大小的多维数组,其大小在运行时确定。make_shared
与( )的 (2) 重载template<class T> shared_ptr<T> make_shared(std::size_t N) // T is U[]
。但是,我面临编译错误(日志如下)。shared
如果我将s 更改为其对应项,则不会出现该错误unique
。我的问题是,
unique
有效?最小工作示例:
\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
(为简洁起见,省略了注释)
In file included …
Run Code Online (Sandbox Code Playgroud) 所以我们的讲师只是向我们展示了如何连接两个链接列表然后继续并向我们展示了如果实现了将会出错的示例,我似乎并不完全理解为什么它不能正常工作,因为它不是解释.如果我以这种方式连接两个链表,会出现什么问题:
template <typename T>
void LList<T>::concat(LList<T> const & list) {
end->next = list.start;
end = list.end;
}
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我这里发生了什么吗?
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++ 程序,可以为文件中的所有字符创建霍夫曼代码。它运行良好,但我想在不使用 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++ ×12
c++11 ×2
new-operator ×2
shared-ptr ×2
c++14 ×1
c++20 ×1
exception ×1
linked-list ×1
raii ×1
string-view ×1
unique-ptr ×1
vector ×1