Versions Compared

Key

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

...

Accessing two iterators which do not refer into the same container or accessing invalidated iterators results in undefined behavior.

...

Code Block
bgColor#FFcccc
langcpp
#include <algorithm>
#include <iostream>
#include <vector>
 
void f(const std::vector<int> &C) {
  std::for_each(C.end(), C.begin(), [](int I) { std::cout << I; });

}

Invalid iterator ranges can also result from comparison functions that return true for equal values. See CTR40-CPP. Provide a valid ordering predicate for more information about comparators.

...

Related Guidelines

...

[ISO/IEC 14882-2014]

24, "Iterators Library"
25.3, "Mutating Sequence Operations" 

[Meyers 01]Item 32, "Follow remove-like algorithms with erase if you really want to remove something"