2

If I have two vector containing std::unique_ptr<>, is there a way to add vector b to the end of vector a, thereby deleting vector b?

For example:

std::unique_ptr<std::vector<int>> a(&someintvector);

std::unique_ptr<std::vector<int>> b(&someotherintvector);

How would I go about moving vector b to the end of vector a?

qwesr
  • 59
  • 1
  • 7
  • 1
    Maybe [this](http://stackoverflow.com/q/9778238/893693) helps you but I am not really sure what the role of the `unique_ptr` is in your code. – Stephan Dollberg Oct 11 '14 at 18:24

2 Answers2

6

You move the contents of b into a:

std::move(std::begin(*b), std::end(*b), std::back_inserter(*a));
Columbo
  • 60,038
  • 8
  • 155
  • 203
David G
  • 94,763
  • 41
  • 167
  • 253
  • Does this work if the vector contains a class too? Because I'm getting the following error: no instance of overloaded function "std::begin" matches the argument list argument types are: (std::unique_ptr>, std::default_delete>>>) – qwesr Oct 11 '14 at 18:27
4

Another way to move the elements to a:

a->insert(a->end(), std::make_move_iterator(b->begin()), std::make_move_iterator(b->end()));
Columbo
  • 60,038
  • 8
  • 155
  • 203