Avoiding iterator invalidation using indices, maintaining clean interface

前端 未结 4 697
悲&欢浪女
悲&欢浪女 2021-02-02 02:24

I have created a MemoryManager class which is basically a wrapper around two vectors of pointers that manage lifetime of heap-allocated objects.

O

4条回答
  •  日久生厌
    2021-02-02 02:59

    You could avoid moving elements of the container by maintaining a free-list (see http://www.memorymanagement.org/glossary/f.html#free.list).

    To avoid invalidation of references to elements you can use a std::deque if you do not insert or erase in the middle. To avoid invalidation of iterators you can use a std::list.

    (Thanks Howard Hinnant)

提交回复
热议问题