Swapnil
Swapnil

Reputation: 401

How Reverse Iterator in C++ works

am beginner to c++ and i want to know how operator ++ moves iterator backward. As i know iterator.begin() and iterator.end() returns pointer to fist index and last index respectively.

vector<int>::iterator it = myvector.begin();

when we do it++ it will move to next index. this is clear to me but i am completely confused with reverse iterator.

vector<int>::reverse_iterator rit = myvector.rbegin();

when we do rit++ it will move to backward. I want know how this is implemented in case of reverse iterator. Is it operator overloading or something which i don't know. Please give me right way to understand these things.

please give more detail knowledge.

Upvotes: 2

Views: 4197

Answers (6)

Emilio Garavaglia
Emilio Garavaglia

Reputation: 20730

_***************_
 ^begin ++--->  ^end
^rend <---++   ^rbegin

Upvotes: 2

Laura Maftei
Laura Maftei

Reputation: 1863

The relationship between a reverse iterator r and the iterator i it is constructed from is:

&*r == &*(i-1)

If end is the one-past-the-end element in a sequence, then the first element in the reversed sequence points to *(end – 1 )

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106076

As i know iterator.begin() and iterator.end() returns pointer to fist index and last index respectively.

Close, but not necessarily:

  • they return iterators, not pointers....

  • end() returns an iterator that's notionally "past" the last valid index.

Internally the vector iterators normally do store pointers though, and a ++ or -- on the iterator goes through a forwarding overloaded operator to perform a corresponding ++ or -- on the pointer.

For reverse iterators, the overloaded operator++ simply performs a -- on the pointer, and vice versa. For example, the Visual C++ library on my computer does this in class _Revranit, from which reverse_iterator is derived:

    _Myt&  operator++()
            {
            --current;
            return (*this);
            }

    _Myt  operator++(int)
            {
            _Myt _Tmp = *this;
            --current;
            return (_Tmp);
            }

Upvotes: 2

Marco A.
Marco A.

Reputation: 43662

Yes, there's a reverse_iterator::operator++ overload which advances "backwards" from the last element to one-before-the-beginning.

The behavior is equivalent to (the page on cppreference might contain another possible implementation)

    reverse_iterator operator++(int)
    {
      reverse_iterator __tmp(*this); 
      --current; // This is an internal iterator
      return __tmp;
    }

Notice that how this is exactly accomplished might vary from implementation to implementation. As far as you comply with the standard requirements, you have some degrees of freedom.

Upvotes: 1

Keiji
Keiji

Reputation: 1042

When you do it++ or rit++ they don't really mean "move forward or backward in the collection", they both mean "move to the next element you want to iterate over".

If you use begin(), you're saying "I want to iterate forwards", so it++ moves forwards.

If you use rbegin(), you're saying "I want to iterate backwards", so rit++ moves backwards.

Upvotes: 2

NPE
NPE

Reputation: 500217

Yes, this is operator overloading. When you do rit++, the compiler internally translates that do

rit.operator++(0);

Here, operator++ is a method of the reverse iterator class that knows how to move the iterator. The dummy integer argument is needed to differentiate between prefix and postfix operator++.

Upvotes: 1

Related Questions