在 unique_ptr 向量中使用 push_back(std::move())
Using push_back(std::move()) in unique_ptr vector
您好,我一直在学习 C++,并且遇到了 unique_ptr。
我想在此向量中输入一个整数。
我使用 vector 因为我也想练习迭代器...
auto integerArray = std::vector<std::unique_ptr<int[]>>(10);
std::cout << "Created:" << sizeof(integerArray)/sizeof(int) << std::endl;
for (int i = 0; i<10;i++) {
int num = i * 10;
integerArray.push_back((std::vector<std::unique_ptr<int>>)std::move(num)); //compile error here
}
std::cout << "Output : ";
for (auto const& i : integerArray) {
std::cout << i << std::endl;
}
这是错误:
error C2664: 'void std::vector<std::unique_ptr<int [],std::default_delete<int []>>,std::allocator<std::unique_ptr<int [],std::default_delete<int []>>>>::push_back(const _Ty &)': cannot convert argument 1 from 'std::vector<std::unique_ptr<int,std::default_delete<int>>,std::allocator<std::unique_ptr<int,std::default_delete<int>>>>' to 'const _Ty &'
with
[
_Ty=std::unique_ptr<int [],std::default_delete<int []>>
]
note: Reason: cannot convert from 'std::vector<std::unique_ptr<int,std::default_delete<int>>,std::allocator<std::unique_ptr<int,std::default_delete<int>>>>' to 'const _Ty'
with
[
_Ty=std::unique_ptr<int [],std::default_delete<int []>>
]
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
vector(633): note: see declaration of 'std::vector<std::unique_ptr<int [],std::default_delete<int []>>,std::allocator<std::unique_ptr<int [],std::default_delete<int []>>>>::push_back'
以及为什么第一个 cout 输出显示 4 而不是 10?我期望有 10 个,因为我制作了一个包含 10 个可以包含整数的块的矢量容器。
您误解了 std::vector
的工作原理。它已经管理其内容,std::unique_ptr
在这种情况下不合适。您还声明指向 int
的指针也是 unique_ptr
中的类型。
如果您只需要一个整数容器,这就是必需的:
auto integerVector = std::vector<int>(10); // filled with 10 default-init values, 0 for int
std::cout << "Created:" << integerVector.size() << std::endl;
for (int i = 0; i<10;i++) {
int num = i * 10;
integerVector.push_back(num); // Adds it to the end, not just first "unfilled"
}
std::cout << "Output : ";
for (auto const& i : integerVector) {
// Will print 20 times, as original 10 "0", and the
// 10 you added to the end
std::cout << i << std::endl;
}
在使用名称“array”和“vector”时要格外小心,因为它们在 C++ 中不是同一回事。它们很相似,但一点也不相同。
您好,我一直在学习 C++,并且遇到了 unique_ptr。 我想在此向量中输入一个整数。 我使用 vector 因为我也想练习迭代器...
auto integerArray = std::vector<std::unique_ptr<int[]>>(10);
std::cout << "Created:" << sizeof(integerArray)/sizeof(int) << std::endl;
for (int i = 0; i<10;i++) {
int num = i * 10;
integerArray.push_back((std::vector<std::unique_ptr<int>>)std::move(num)); //compile error here
}
std::cout << "Output : ";
for (auto const& i : integerArray) {
std::cout << i << std::endl;
}
这是错误:
error C2664: 'void std::vector<std::unique_ptr<int [],std::default_delete<int []>>,std::allocator<std::unique_ptr<int [],std::default_delete<int []>>>>::push_back(const _Ty &)': cannot convert argument 1 from 'std::vector<std::unique_ptr<int,std::default_delete<int>>,std::allocator<std::unique_ptr<int,std::default_delete<int>>>>' to 'const _Ty &'
with
[
_Ty=std::unique_ptr<int [],std::default_delete<int []>>
]
note: Reason: cannot convert from 'std::vector<std::unique_ptr<int,std::default_delete<int>>,std::allocator<std::unique_ptr<int,std::default_delete<int>>>>' to 'const _Ty'
with
[
_Ty=std::unique_ptr<int [],std::default_delete<int []>>
]
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
vector(633): note: see declaration of 'std::vector<std::unique_ptr<int [],std::default_delete<int []>>,std::allocator<std::unique_ptr<int [],std::default_delete<int []>>>>::push_back'
以及为什么第一个 cout 输出显示 4 而不是 10?我期望有 10 个,因为我制作了一个包含 10 个可以包含整数的块的矢量容器。
您误解了 std::vector
的工作原理。它已经管理其内容,std::unique_ptr
在这种情况下不合适。您还声明指向 int
的指针也是 unique_ptr
中的类型。
如果您只需要一个整数容器,这就是必需的:
auto integerVector = std::vector<int>(10); // filled with 10 default-init values, 0 for int
std::cout << "Created:" << integerVector.size() << std::endl;
for (int i = 0; i<10;i++) {
int num = i * 10;
integerVector.push_back(num); // Adds it to the end, not just first "unfilled"
}
std::cout << "Output : ";
for (auto const& i : integerVector) {
// Will print 20 times, as original 10 "0", and the
// 10 you added to the end
std::cout << i << std::endl;
}
在使用名称“array”和“vector”时要格外小心,因为它们在 C++ 中不是同一回事。它们很相似,但一点也不相同。