Pointer to elements in STL containers

Dear all,

STL containers are inevitable to implement sequential or linked data collection. To improve efficiency of programs, I often consider to refer their elements by using the pointer(or reference).

As you know, std::vector entails transfer of entire memory domains when calling push_back has no capacity, which makes troubles in permanent safe reference.

Is permanency of memory address of each element in STL containers determined in the specification of the C++ standard? (Or does their behavior differ in library's implementations?)
> Is permanency of memory address of each element in STL containers determined
> in the specification of the C++ standard?

Yes.

For example,

vector push_back:
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.
https://en.cppreference.com/w/cpp/container/vector/push_back


list push_back:
No iterators or references are invalidated.
https://en.cppreference.com/w/cpp/container/list/push_back
Dear JLBorges,

Thank you for your rapid and accurate reply.

Today, I feel understanding the usage of std::list for the first time.

Thank you so much
Last edited on
Topic archived. No new replies allowed.