Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare C++ STL list iterators

Tags:

c++

list

stl

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 ?

like image 630
Jose Shaidmann Avatar asked Dec 15 '22 17:12

Jose Shaidmann


2 Answers

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.

like image 139
Mike Seymour Avatar answered Jan 05 '23 12:01

Mike Seymour


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

like image 37
Kos Avatar answered Jan 05 '23 13:01

Kos