Fix location STL/boost containers?
Dear list, I need direct access ( save and use element pointers) to container elements in my application. std::vector<obj> can not be used since vector::push_back etc may relocate objs. std::list<obj> seems to be working but I could not find any guarantee that an element will not be moved around because of operations on other elements. Is there a std or boost container that fits my need? Many thanks in advance. Bo
"Bo Peng"
I need direct access ( save and use element pointers) to container elements in my application. std::vector<obj> can not be used since vector::push_back etc may relocate objs.
Just a suggestion: Can you, instead of storing objects in your vector, allocate the objects on the heap and store pointers (or shared pointers) to them? In this case you would not have to worry about the vector moving it's elements around -- it would be just moving pointers. Regards, Arkadiy
Can you, instead of storing objects in your vector, allocate the objects on the heap and store pointers (or shared pointers) to them? In this case you would not have to worry about the vector moving it's elements around -- it would be just moving pointers.
This may be a good solution. The only problem is that find(...) will be looking for identical pointers instead of identical objects. Easy to fix though. Thank you very much. Bo
Bo Peng wrote:
Can you, instead of storing objects in your vector, allocate the objects on the heap and store pointers (or shared pointers) to them? In this case you would not have to worry about the vector moving it's elements around -- it would be just moving pointers.
This may be a good solution. The only problem is that find(...) will be looking for identical pointers instead of identical objects. Easy to fix though.
if nothing is even going to change in the container, do you know the number of items at compile time? If so, you could perhaps use boost::array. Cheers Russell
if nothing is even going to change in the container, do you know the number of items at compile time? If so, you could perhaps use boost::array.
Items will (only) be added, removed and searched. Order of items does not matter so any container should work, as long as it does not move items around once they are added. Bo
On 4/22/05, Bo Peng
Can you, instead of storing objects in your vector, allocate the objects on the heap and store pointers (or shared pointers) to them? In this case you would not have to worry about the vector moving it's elements around -- it would be just moving pointers.
This may be a good solution. The only problem is that find(...) will be looking for identical pointers instead of identical objects. Easy to fix though.
Thank you very much. Bo
Easiest to fix using indirect_iterators (http://www.boost.org/libs/iterator/doc/indirect_iterator.html) rather than raw container iterators... Stuart Dootson
This may be a good solution. The only problem is that find(...) will be looking for identical pointers instead of identical objects. Easy to fix though.
Easiest to fix using indirect_iterators (http://www.boost.org/libs/iterator/doc/indirect_iterator.html) rather than raw container iterators...
This is *exactly* what I need! Thank you! Bo
"Bo Peng"
participants (5)
-
Arkadiy Vertleyb
-
Bo Peng
-
Russell Hind
-
Stuart Dootson
-
Thorsten Ottosen