Is it really for comparing iterators obtained from a container separately?

For example, is this expression true in semantic?

container.begin() == container.begin(); 
+6
c ++ stl
source share
2 answers

Yes, if no iterator was declared invalid.

For example, the following is not 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. 
+11
source share

Yes, begin() will return the same iterator defined by the container instance unless you somehow modify the container ( end() also has this property). For example, std::vector::push_back() may cause the array to be redistributed to accommodate new elements.

+4
source share

All Articles