I have objects with intrusive list hooks. I want to manage memory by keeping them in a std开发者_JAVA百科 collection. I then use the intrusive collections to perform operations on the objects (sorting etc...).
intrusive::list<MyObject> intrusiveList;
vector<MyObject> stdVector;
stdVector.push_back(MyObject());
intrusiveList.push_back(stdVector.front());
vector is not a good option (unless you can resize up front) because it may copy the objects to different memory which invalidates any intrusive hooks. What about deque? For those not familiar with boost::intrusive the question really boils down to this: Can deque ever recopy an object into different memory or does it guarantee to stay in the same memory for the life of the deque? I know there are some guarantees about back() and front(), but it does not guarantee all iterators can never be invalidated so I'm a little confused.
A deque will only copy objects if you insert or delete in the middle the deque. You can add or delete at both ends without making any objects move.
Believe though that iterators might be invalidated, even if the address of each member remains the same. A deque has a two-level structure and some internal data might be reallocated, invalidating iterators into the deque.
More precisely:
An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque. An insertion at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.
精彩评论