Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all end() iterators equivalent for a collection type?

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...
}
like image 730
WilliamKF Avatar asked Jan 29 '13 21:01

WilliamKF


1 Answers

No, because of the STL container and iterator requirements:

23.2.1 General container requirements [container.requirements.general]

6 begin() returns an iterator referring to the first element in the container. end() returns an iterator which is the past-the-end value for the container. If the container is empty, then begin() == end();

24.2.1 In general [iterator.requirements.general]

6 An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to elements of the same sequence.

The equality of begin() and end() for empty containers means that begin() and end() need to be part of the same container objects, and hence end() cannot be a static member of a container class. Note also that -except for forward iterators- applying operator-- on end() would be impossible to resolve with a static end() iterator.

like image 129
TemplateRex Avatar answered Oct 04 '22 21:10

TemplateRex