Persistent links in STL containers

When using C ++ STL containers, under what conditions do I need to access reference values? For example, are any links invalid after the next function call to the container?

{ std::vector<int> vector; vector.push_back (1); vector.push_back (2); vector.push_back (3); vector[0] = 10; //modifies 0'th element int& ref = vector[0]; ref = 10; //modifies 0'th element vector.push_back (4); ref = 20; //modifies 0'th element??? vector.clear (); ref = 30; //clearly obsurd } 

I understand that in most stl implementations this will work, but I'm interested in what a standard declaration requires.

- edit: I'm interested because I wanted to try STXXL ( http://stxxl.sourceforge.net/ ) for C ++, but I realized that the links returned by the containers were not constant over multiple reads and therefore incompatible without making changes (as if superficially) to my existing stl code. Example:

 { std::vector<int> vector; vector.push_back (1); vector.push_back (2); int& refA = vector[0]; int& refB = vector[1]; //refA is not gaurenteed to be valid anymore } 

I just wanted to know if this means that the STXXL containers where are not 100% compatible, or really, if I used the STL containers all the time in an unsafe / implementation-dependent way.

+6
c ++ stl
source share
4 answers

About insertion into vectors, the standard says in 23.2.4.3/1:

[ insert() ] causes redistribution if the new size is larger than the old capacity. If redistribution does not occur, all iterators and references before the insertion point remains valid.

(Although this actually indicates insert() , table 68 states that a.push_back(x) should be equivalent to a.insert(a.end(), x) for any vector a and value x .) This means that if reserve() enough memory, then (and only then) iterators and links will not be guaranteed to be invalidated if you insert() or push_back() more elements.

Regarding the removal of elements, 23.2.4.3/3 says:

[ erase() ] invalidates all iterators and references after the erase point.

According to table 68 and table 67, respectively, pop_back() and clear() equivalent to the corresponding calls to erase() .

+12
source share

Some basic rules for the vector:

  • Redistribution cancels all references, pointers, and iterators for vector elements.
  • Insertions can invalidate links, pointers, and iterators.
  • Inserting or deleting elements invalidates links, pointers, and iterators that reference the following elements.
  • If the insert causes a redistribution, it will invalidate all references, iterators, and pointers.
+7
source share

I expect links to be invalidated only with any explicit or implicit resize() (see also max_size , capacity and reserve methods).

+1
source share

A vector invalidates its iterator and links when redistributing, depending on its current capacity. Although the above code may work in some cases, you should not rely on this since the link may not be valid after calling push_back (4).

+1
source share

All Articles