c++ - 如何处理 C++0x STL 中丢失的 'emplace_range'?

标签 c++ algorithm c++11 move-semantics

我有两个容器,假设它们是这样定义的:

std::vector<std::unique_ptr<int>> a;
std::vector<std::unique_ptr<int>> b;

假设 ab 都被填充了。我想使用 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/

相关文章:

Java算法题

iphone - 由 json 文件 iphone 制作的单层的 A* 寻路算法

android - 是否可以在 gcc 编译器中使用 clang std 库?

c++ - 广义 lambda 捕获的宏

c++ - 我怎样才能返回一个对象,它自己在 C++ 中的堆上分配了空间?

c++ - 不能向侧面动态施放

c++ - mmap 和 mutex 的使用

C++ 如何在没有 Win32 API 的情况下制作一个简单的窗口?

c# - 如何找到对整数数组进行排序的最小步骤数

c++ - std::array 的内联初始化有什么问题?