dskoda1
dskoda1

Reputation: 109

C++ iterator equality

How does c++ std library implement for example, vector iterators? It allows you to do equality testing on a regular iterator and a reverse iterator, what kind of design allows this to be possible?

Edit: So below someone responded with the actual code of overloaded == method, which is not what i'm looking for, so maybe the scope of this question wasn't clear. What I mean by "how does the std library implement vector iterators" is more of a design level question of what kind of inheritance tree does the iterator class use, what were some design trade-offs with doing it that way, other alternative designs, etc. If someone could shed some light on this that would be great.

Upvotes: 1

Views: 480

Answers (1)

Adi Levin
Adi Levin

Reputation: 5233

This is from <vector>:

bool operator==(const _Myiter& _Right) const
    {   // test for iterator equality
        _Compat(_Right);
         return (this->_Ptr == _Right._Ptr);
    }

The iterator contains a pointer _Ptr as a member. This is typical of all iterators. They are wrappers to a reference into the collection.

Upvotes: 2

Related Questions