Reverse Iterators


Reverse iterators are just like regular iterators, except they traverse a collection backward instead of forward. Every first-class container supports the following functions for obtaining a reverse iterator positioned at an extremity.

rbegin
reverse_iterator rbegin()
Returns a reverse iterator positioned at the last item in the collection.
rbegin
const_reverse_iterator rbegin() const
Returns a const reverse iterator positioned at the last read-only item in the collection.
rend
reverse_iterator rend()
Returns a reverse iterator positioned immediately before the first item in the collection.
rend
const_reverse_iterator rend() const
Returns a const reverse iterator positioned immediately before the first read-only item in the collection.

The following example uses a reverse iterator for iterating backward through a vector of strings.

Example <ospace/osstd/ examples/iter3.cpp>
#include <iostream>
#include <vector>

void
main()
  {
  vector< char* > v; // Vector of character strings.
  v.push_back( "zippy" );
  v.push_back( "motorboy" );

  vector< char* >::reverse_iterator i;
  for ( i = v.rbegin(); i != v.rend(); ++i )
    cout << *i << "\n";
  }

motorboy
zippy

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.