Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is STL iterator equality established?

Tags:

c++

stl

I was wondering, how is equality (==) established for STL iterators? Is it a simple pointer comparison (and thus based on addresses) or something more fancy?

If I have two iterators from two different list objects and I compare them, will the result always be false?

What about if I compare a valid value with one that's out of range? Is that always false?

like image 376
Daniel Avatar asked May 10 '09 05:05

Daniel


People also ask

What are the STL iterators and what is their purpose?

An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container.

Which STL function returns an iterator?

end(): The end() function returns an iterator pointing to the past-the-last element of the container.

How is iterator implemented in C++?

The iterator is implemented as a pointer to a node, and contains operator overloads for the four usual iterator operations of dereference, increment, comparison, and assignment. in the list class that can be used to insert new data items at arbitrary locations in the list.

What are the different types of iterators used in STL?

Most standard STL containers at least support forward iterators. Bidirectional Iterators: These are similar to forward iterators with the only difference that they are bidirectional. This means that we can use these bidirectional iterators to traverse through the range in the forward as well as backward direction.


2 Answers

Iterator classes can define overloaded == operators, if they want. So the result depends on the implementation of operator==.

You're not really supposed to compare iterators from different containers. I think some debug STL implementations will signal a warning if you do this, which will help you catch cases of this erroneous usage in your code.

like image 178
Chris Jester-Young Avatar answered Oct 07 '22 20:10

Chris Jester-Young


Daniel asked: I was wondering, how is equality (==) established for STL iterators? Is it a simple pointer comparison (and thus based on addresses) or something more fancy?

It depends on implementation. Right now, on Visual C++ 2008, I see the following code (for the list iterator):

bool operator==(const _Myt_iter& _Right) const
{   // test for iterator equality

#if _HAS_ITERATOR_DEBUGGING
    _Compat(_Right);
#else
    _SCL_SECURE_TRAITS_VALIDATE(this->_Has_container() && this->_Same_container(_Right));
#endif /* _HAS_ITERATOR_DEBUGGING */

    return (_Ptr == _Right._Ptr);
}

You'll see above that there is both code for verification of iterator validity, and _Ptr being a pointer to a list node.

So I guess there is both verification, and simple, raw pointer comparison.

Daniel asked: If I have two iterators from two different list objects and I compare them, will the result always be false?

Until now, it appears the standard was somewhat unclear on the subject. Apparently, they will explicitly write that this kind of operation has undefined results:

Quoting: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#446

The result of using any iterator operation (24.2.1 [input.iterators], 24.2.2 [output.iterators], 24.2.3 [forward.iterators], 24.2.4 [bidirectional.iterators], 24.2.5 [random.access.iterators]) that uses two iterator values as arguments (footnote) which were obtained from two different ranges r1 and r2 (including their past-the-end values) which are not subranges of one common range is undefined, unless explicitly described otherwise.

footnote) Among others these operations are ==, <, binary -, and copy assignment

So I guess it is evil to compare iterator from different containers... ^_^

Daniel asked: What about if I compare a valid value with one that's out of range? Is that always false?

Same as above.

like image 44
paercebal Avatar answered Oct 07 '22 22:10

paercebal