I have two containers, let’s say they’re defined like this:
std::vector<std::unique_ptr<int>> a;
std::vector<std::unique_ptr<int>> b;
Assume both a and b are populated. I want to insert the entire container a to a particular location in b, using move-semantics so the unique_ptrs move to b. Let’s assume i is a valid iterator to somewhere in b. The following doesn’t work:
b.insert(i, a.begin(), a.end()); // error: tries to copy, not move, unique_ptrs
Is there another STL algorithm that can achieve this ‘insert-range-by-moving’? I guess I need a sort of emplace_range, but there isn’t one in VS2010’s STL. I don’t want to write a loop that inserts one by one, since it would end up a nasty O(n^2) due to shifting up the entire contents of the vector every time it inserts. Any other options?
1 Answer