If you absolutely want to use
boost::ptr_vector, all you have to do is create indirect iterators. For example, something like this works:
Code:
#include <algorithm>
#include <iostream>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/noncopyable.hpp>
using std::cout;
using std::endl;
using std::random_shuffle;
using boost::noncopyable;
using boost::ptr_vector;
class Simple : private noncopyable {
public:
int a;
Simple(int a = 0) : a(a) { }
};
const int max=10;
int main() {
ptr_vector<Simple> svec;
ptr_vector<Simple>::iterator svit;
for(int i = 0; i < max; i++)
svec.push_back(new Simple(i));
random_shuffle(svec.begin().base(), svec.end().base());
for(svit = svec.begin(); svit != svec.end(); svit++)
cout << svit->a << endl;
for(int i = 0; i < max; i++)
delete svec.pop_back().release();
return 0;
}
Notice the line in
red. If we remove both instances of “
.base()”, we get compilation errors telling us that our class cannot be copied.
Also notice that by doing this, you lose the main advantage of using
boost::ptr_vector<Simple> over
std::vector<Simple*> (at least in this situation). I guess it makes sense if you “break the rules” for only one operation but use the advantages of Boost during the rest of the time.