dynamic vector-like container but whose elements save their indexes?
All the elements should have fixed position in the array after insertion until I explicitly delete them from there. Is there something li开发者_StackOverflow社区ke this in boost or wherever? Thanks
Use an unordered_map<int, T>
or map<int, T>
.
Or, use a vector<
optional<T>
>
, and set the slot to delete to none_t
, instead of actually deleting it.
Instead of "deleting" an element, you want to set its value to null (or some other "no value" equivalent). Then everything stays constant as you require.
Interesting. Is your goal is to expose a mapping from integers to SLOTS, where those SLOTS may contain a value? Or is your goal to preserve the underlying address of each element and the underlying address of the start of the internal array itself? Presumably you have a reason that you need either the position of the elements or the mapping from integer keys to elements to be preserved after an element is "removed". What is that reason?
The map<>
or vector<>
implementations mentioned above may not work because the remove
, erase
, find
, etc. operations will remove, rearrange, or inspect the integers which you consider to be "removed".
Unfortunately, I think this may be a case where you need to roll your own using a wrapper around vector<optional<T> >
or vector<T*>
, depending upon how you define remove
.
精彩评论