Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

ClassFunctionIteratorsReferencesPointersNotes
std::deque     
 insert(), emplace_front(), emplace_back(),
emplace(), push_front(), push_back()
XX 

An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque. An insertion at either end of the deque invalidates all the iterators to the deque but has no effect on the validity of references to elements of the deque. ([deque.modifiers], paragraph 1)

 erase(), pop_back(), resize()XX 

An erase operation that erases the last element of a deque invalidates only the past-the-end iterator and all iterators and references to the erased elements. An erase operation that erases the first element of a deque but not the last element invalidates only the erased elements. An erase operation that erases neither the first element nor the last element of a deque invalidates the past-the-end iterator and all iterators and references to all the elements of the deque. ([deque.modifiers], paragraph 4)

 clear()XXX

Destroys all elements in the container. Invalidates all references, pointers, and iterators referring to the elements of the container and may invalidate the past-the-end iterator. ([sequence.reqmts], Table 100)

std::forward_list     
 erase_after(), pop_front(), resize()XX erase_after shall invalidate only iterators and references to the erased elements. ([forwardlist.modifiers], paragraph 1)
 remove(), unique()XX 

Invalidates only the iterators and references to the erased elements. ([forwardlist.ops], paragraph 12 & paragraph 16)

 clear()XXXDestroys all elements in the container. Invalidates all references, pointers, and iterators referring to the elements of the container and may invalidate the past-the-end iterator. ([sequence.reqmts], Table 100)
std::list     
 erase(), pop_front(), pop_back(), clear(), remove(), remove_if(), unique()XX Invalidates only the iterators and references to the erased elements. ([list.modifiers], paragraph 3 and [list.ops], paragraph 15 & paragraph 19)
 clear()XXXDestroys all elements in the container. Invalidates all references, pointers, and iterators referring to the elements of the container and may invalidate the past-the-end iterator. ([sequence.reqmts], Table 100)
std::vector     
 reserve()XXX

After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens and equal to the previous value of capacity() otherwise. Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. ([vector.capacity], paragraph 3 & paragraph 6)

 insert(), emplace_back(), emplace(), push_back()XX 

Causes reallocation if the new size is greater than the old capacity. If no reallocation happens, all the iterators and references before the insertion point remain valid. ([vector.modifiers], paragraph 1). All iterators and references after the insertion point are invalidated.

 erase(), pop_back(), resize()XX Invalidates iterators and references at or after the point of the erase. ([vector.modifiers], paragraph 3)
 clear()XXXDestroys all elements in the container. Invalidates all references, pointers, and iterators referring to the elements of the container and may invalidate the past-the-end iterator. ([sequence.reqmts], Table 100)
std::set, std::multiset, std::map, std::multimap     
 erase(), clear()XX Only invalidates iterators and references to the erased elements. ([associative.reqmts], paragraph 9)

std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap

     
 erase(), clear()XX Only invalidates Invalidates only iterators and references to the erased elements. ([unord.req], paragraph 14)
 insert(), emplace()X  

The insert and emplace members shall not affect the validity of iterators if (N+n) < z * B, where N is the number of elements in the container prior to the insert operation, n is the number of elements inserted, B is the container’s bucket count, and z is the container’s maximum load factor. ([unord.req], paragraph 15)

 rehash(), reserve()X  

Rehashing invalidates iterators, changes ordering between elements, and changes which buckets the elements appear in but does not invalidate pointers or references to elements. ([unord.req], paragraph 9)

std::valarrayresize() XXResizing invalidates all pointers and references to elements in the array. ([valarray.members], paragraph 12)

...

In this compliant solution, pos is assigned a valid iterator on each insertion, removing the undefined behavior:

Code Block
bgColor#ccccff
langcpp
#include <deque>
 
void f(const double *items, std::size_t count) {
  std::deque<double> d;
  auto pos = d.begin();
  for (std::size_t i = 0; i < count; ++i, ++pos) {
    pos = d.insert(pos, items[i] + 41.0);
  }
}

...

In this noncompliant code example, data is invalidated after the call to replace(), and so its use in g() is undefined behavior:

Code Block
bgColor#ffcccc
langcpp
#include <iostream>
#include <string>
 
extern void g(const char *);
 
void f(std::string &example_string) {
  const char *data = example_string.data();
  // ...
  example_string.replace(0, 2, "bb");
  // ...
  g(data);

}

...

[ISO/IEC 14882-2014]

23, "Containers Library"
24.2.1, "In General" 

[Meyers 01]Item 43: , Prefer algorithm calls to hand-written loops
[Sutter 04]Item 84: , Prefer algorithm calls to handwritten loops
[Kalev 99]ANSI/ISO C++ Professional Programmer's Handbook

...