Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::equal with reverse_iterator

Tags:

c++

stl

Is it legal to use a reverse_iterator with std::equal?

For example, are any of these legal?

std::equal(v.begin(), v.end(), w.rbegin())

std::equal(v.rbegin(), v.rend(), w.begin())

std::equal(v.rbegin(), v.rend(), w.rbegin())
like image 275
Thomas Eding Avatar asked May 30 '12 18:05

Thomas Eding


People also ask

How to reverse iterator in C++?

C++ Iterators Reverse Iterators A reverse iterator is made from a bidirectional, or random access iterator which it keeps as a member which can be accessed through base() . To iterate backwards use rbegin() and rend() as the iterators for the end of the collection, and the start of the collection respectively.

How do you reverse a map in C++?

The C++ function std::map::rbegin() returns a reverse iterator which points to the last element of the map. Reverse iterator iterates in reverse order that is why incrementing them moves towards beginning of map.

What is Rbegin?

The rbegin() is a function in C++ STL. It returns a reverse iterator which points to the last element of the map. The reverse iterator iterates in reverse order and incrementing it means moving towards beginning of map.

What is a bidirectional iterator?

Description. A Bidirectional Iterator is an iterator that can be both incremented and decremented. The requirement that a Bidirectional Iterator can be decremented is the only thing that distinguishes Bidirectional Iterators from Forward Iterators.


1 Answers

All are valid, because reverse iterators are, in fact, forward iterators.

"Reverse iterator" is not an iterator category. Remember some iterator categories:

  • An iterator that can be dereferenced (*) and incremented (++) is a forward iterator.
  • A forward iterator that can also be decremented is a bidirectional iterator.
  • A random access iterator is a biderectional iterator that also has + and - operators.

On the other hand, a reverse iterator is a bidirectional iterator or a random access iterator that looks at a collection in reverse. Look at

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

... especially what it says about iterator_category under the "Member types" heading.

like image 68
wolfgang Avatar answered Oct 05 '22 22:10

wolfgang