我有两个容器,假设它们是这样定义的:
std::vector<std::unique_ptr<int>> a;
std::vector<std::unique_ptr<int>> b;
假设 a
和 b
都被填充了。我想使用 move 语义将整个容器 a
插入到 b
中的特定位置,以便 unique_ptr
move 到 b
。假设 i
是指向 b
中某处的有效迭代器。以下不起作用:
b.insert(i, a.begin(), a.end()); // error: tries to copy, not move, unique_ptrs
是否有另一种 STL 算法可以实现这种“move 插入范围”?我想我需要一种 emplace_range
,但 VS2010 的 STL 中没有。我不想编写一个一个一个插入的循环,因为每次插入时都会向上 move vector 的全部内容,因此最终会导致令人讨厌的 O(n^2) 。还有其他选择吗?
最佳答案
auto a_begin = std::make_move_iterator(a.begin());
auto a_end = std::make_move_iterator(a.end());
b.insert(i, a_begin, a_end);
关于c++ - 如何处理 C++0x STL 中丢失的 'emplace_range'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4186913/