4 ms·
This is why things like std::list still have value: iterator stability. You even get this in most map/sets too. But vector is usually what one should reach for
by _huayra_ 5y ago
This is why things like std::list still have value: iterator stability. You even get this in most map/sets too.
But vector is usually what one should reach for unless one has a good reason not to. The only danger really comes when the lifetime of iterators and the thing they point to become a bit decoupled, even due to insertion!
- einpoklum 5y agoYou can also use an offset instead of a pointer (or a pointer to the object and and an offset into its heap storage, or whatever).
- _huayra_ 5y agoThis is actually on my list of "neat utilities to write": an iterator that encapsulates this "offset" for vector. If anyone is looking to write a custom iterator, I definitely recommend Boost's new stl interfaces: https://github.com/boostorg/stl_interfaces https://github.com/boostorg/stl_interfaces
- nly 5y agoOr you could just use Boost stable_vector and save yourself the trouble https://www.boost.org/doc/libs/1_76_0/doc/html/boost/container/stable_vector.html https://www.boost.org/doc/libs/1_76_0/doc/html/boost/contain...
- gpderetta 5y agodeque is also good for reference stability when doing push_backs (but of course not when inserting in the middle).