c++ boost::ptr_container and std::random_shuffle
Hello, I have a C++ problem:
I have a base class for very simple objects, which are however non copyable. And I have containers of these objects : a vector and a tree (for the same objects, hence I need pointers). I use boost::ptr_vector for the vector and I would like to use it as well for the tree but it does not work because I need to std::random_shuffle the order of the objects in the tree. It does not work because iterators on boost::ptr_vector return references and the std::swap used internally by std::random_shuffle panics on non-copyable objects, I actually need to shuffle the order of the pointers and not the order of the objects. Now I use a std::vector of raw pointers but boost is much more convenient. Is there a relatively simple solution to my problem ? (if it is not simple, raw pointers are OK) |
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> 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. |
Thank you! I have just tried and it works! and it is simple, too, I am impressed!
Yes, that is the only place where I should need to "break the rule" and the program gets a little simpler using boost. So this is perfect. Thanks again! |
All times are GMT -5. The time now is 09:27 PM. |