tags:

views:

107

answers:

2

For example, it this expression valid in semantic?

container.begin() == container.begin();
+4  A: 

Yes, begin() will return the same iterator given a container instance, unless you change the container in some way (end() has this property as well). For example, std::vector::push_back() may cause the array to be reallocated to accommodate new elements.

In silico
Be carefull using the term `same`. I don't think all containers will return the `same` iterator for the same item. Though they will compare equal when compared with ==.
Martin York
You're right, I've rephrased my answer.
In silico
David Rodríguez - dribeas
+11  A: 

Yes, so long as neither iterator has been invalidated.

For example, the following would not be valid:

std::deque<int> d;

std::deque<int> begin1 = d.begin();
d.push_front(42);                   // invalidates begin1!
std::deque<int> begin2 = d.begin();
assert(begin1 == begin2);           // wrong; you can't use begin1 anymore.
James McNellis