4

I want to move certain element from a to b:

boost::ptr_vector<Foo> a, b;
// ...
b.push_back(a.release(a.begin() + i)));

The above code does not compile because the release function returns boost::ptr_container_detail::static_move_ptr<...>, which is not suitable for pushing back.

How should I proceed?

EDIT: I found out that the object returned has .get() .release() that provides a raw pointer (that may also lead to some exception safety issues). I would, however, prefer not relying on undocumented internal functionality, so feel free to share any better solutions...

Tronic
  • 10,250
  • 2
  • 41
  • 53

2 Answers2

4
boost::ptr_vector<Foo> a, b;

// transfer one element a[i] to the end of b
b.transfer( b.end(), a.begin() + i, a ); 
// transfer N elements a[i]..a[i+N] to the end of b
b.transfer( b.end(), a.begin() + i, a.begin() + i + N, a );
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
0

Personally, I prefer to use a std::vector<> of boost::shared_ptr (ie std::vector> a, b).

Then you can use the standard vectorfunctions.

Warpin
  • 6,971
  • 12
  • 51
  • 77
  • That approach is simpler and arguably more robust, but the pointer containers are far more efficient if you need to juggle hundreds of thousands of objects. – timday Mar 02 '10 at 13:29