hui*_*arc 1 c++ move vector unique-ptr
有没有办法移动unique_ptr
unique_ptr<int[]> foo;
Run Code Online (Sandbox Code Playgroud)
进入矢量?
vector<int> bar;
Run Code Online (Sandbox Code Playgroud)
不,你不能只是vector提供一块内存,并期望它将其用作阵列存储.一个vector的存储必须来自该分配器vector的用途.
您可以尝试使用allocator gimmicks以vector某种方式将内存伪装成.但即使这样也很棘手,因为无论你告诉它多大,它vector都能分配尽可能多的ints.
您可以将a的内容移动unique_ptr到数组的元素中.说,如果你有vector<unique_ptr<int[]>>.但你不能只是把一块记忆打成一片vector.