Jose Shaidmann
Jose Shaidmann

Reputation: 41

compare C++ STL list iterators

I have a C++ STL list of objects in my app that is built on startup and never altered afterwards; is it the case that two independent iterators that point to the same list node always compare equal ?

Upvotes: 4

Views: 1598

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254751

Yes. One of the requirements for forward iterators is:

C++11 22.4.5/6: If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object.

All iterators over standard containers are (at least) forward iterators.

Upvotes: 10

Kos
Kos

Reputation: 72319

Yes, according to cplusplus.com:

http://www.cplusplus.com/reference/std/iterator/ForwardIterator/

Accepts equality/inequality comparisons.
Equal iterators imply the same element is pointed

(I'm not really a fan of this site but I'd trust it here.)


cppreference.com agrees and states more than that, namely all InputIterators (that can be read from) are EqualityComparable, see:

http://en.cppreference.com/w/cpp/concept/InputIterator

Upvotes: 2

Related Questions