Home > Mobile >  Can reverse iterators be used in place of forward iterator parameters?
Can reverse iterators be used in place of forward iterator parameters?

Time:08-08

Looking at the function parameters for std::generate() the parameters say that this function only takes forward iterators:

I'm having a hard time understanding why this code is compiling:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void printer(int i) {
   cout << i << ", ";
}

struct Sequence {
   int start;
   Sequence(int start) :start(start) {

   }

   int operator() () {
      return start   % 7;
   }
};


int main() {
   vector<int> v1(4);
   generate(v1.rbegin(), v1.rend(), Sequence(10)); // Line I
   for_each(v1.begin(), v1.end(), printer);
}

Outputs 6, 5, 4, 3,

My understanding is that std::generate takes two parameters that need to both be forward iterators.

And looking at the documentation for rbegin() and rend(), they both return reverse iterators. Is there some type of implicit conversion that allows these reverse iterators to act as Forward Iterators?

At first, I suspected that the .base() part of these iterators may be at play

   generate(v1.rbegin().base(), v1.rend().base(), Sequence(10)); //Line II

This compiles but throws an error at run time (Which makes complete sense).

I also tried

generate (v1.rend().base(), v1.rbegin().base(), Sequence(10)); // LINE III

Output : 3, 4, 5, 6,

This compiles and runs but isn't equivalent to my original output (Line I), so this isn't what is happening.

Any help or input would be greatly appreciated.

CodePudding user response:

You know what, reverse-iterators can be forward-iterators too!

Specifically, the term reverse-iterator means that they go reverse to the "natural" direction.

The term forward-iterator names a concept, which guarantees among others that you can make a copy of an iterator, and copy and original can be independently used to iterate the sequence, and look at the elements, however often you want.

Take a good look at it, and you will see that while both are based on the concept iterator, and while they add different additional requirements, those are not contradictory.

Actually, whenever you have a reverse-iterator, they are normally automatically derived by decorating a bidirectional iterator (which is a forward-iterator), though that is not quite a requirement.

  • Related