Given a particular stl collection in C++, is the end() value equivalent for all instances of the same templatization? In other words, will the following work for all stl containers and circumstances (not just for std::map)?
std::map<Key, Value> foo(int seed);
std::map<Key, Value> instance1 = foo(1);
std::map<Key, Value> instance2 = foo(2);
std::map<Key, Value>::iterator itr = instance1.begin();
std::map<Key, Value>::iterator endItr = instance2.end(); // Comes from other collection!
for (; itr != endItr; ++itr) {
// Do something on each key value pair...
}
No, because of the STL container and iterator requirements:
23.2.1 General container requirements [container.requirements.general]
24.2.1 In general [iterator.requirements.general]
The equality of
begin()andend()for empty containers means thatbegin()andend()need to be part of the same container objects, and henceend()cannot be a static member of a container class. Note also that -except for forward iterators- applyingoperator--onend()would be impossible to resolve with a staticend()iterator.