Home > Mobile >  Is there a C container that doesn't invalidate references on insertion/deletion?
Is there a C container that doesn't invalidate references on insertion/deletion?

Time:10-08

I used std::vector but it end up invalidating references upon on inserts then I moved to std::deque which works great for inserts, but now the problem is that if I delete something middle of it, it end up invalidating the rest references.

Is there any container that doesn't invalidate references on both insertion and deletion?

If not, How can I achieve what I want?

CodePudding user response:

std::list might do what you want -- insertion does not invalidate any references or iterators on the list, and deletion just invalidates references/iterators pointing at the element deleted.

CodePudding user response:

Is there a C container that doesn't invalidate references on insertion/deletion?

All node based containers are such. They invalidate only references to the removed element on remove, and the end iterator in case of both operations.

The linked lists and the associative containers are node based.

moved to std::deque which works great for inserts, but now the problem is that if I delete something middle of it, it end up invalidating the rest references.

There's no difference for invaldation of insert and removal operations of deque. In either case, if you operate on the ends, then no references are invalidated (except end, begin, and removed element) and if you operate in the middle, then references are invalidated.

  •  Tags:  
  • c
  • Related