...
- both iterators refer into the same container,
 - the iterator representing the start of the range precedes the iterator representing the end of the range,
 - the elements iterated over do not have unspecified values, and
 - the iterators are not invalidated, in conformance with CTR32CTR51-CPP. Use valid references, pointers, and iterators to reference elements of a container.
 
Accessing two iterators which do not refer into the same container or accessing invalidated iterators results in undefined behavior.
...
| Code Block | ||||
|---|---|---|---|---|
  | ||||
#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
| CERT C++ Coding Standard | CTR32CTR51-CPP. Use valid references, pointers, and iterators to reference elements of a container CTR40-CPP. Provide a valid ordering predicate  | 
...
| [ISO/IEC 14882-2014] | 24, "Iterators Library"  | 
| [Meyers 01] | Item 32, "Follow remove-like algorithms with erase if you really want to remove something" |