public member function
<iterator>

std::reverse_iterator::operator+

reverse_iterator operator+ (difference_type n) const;
Addition operator
Returns a reverse iterator pointing to the element located n positions away from the element the iterator currently points to.

Internally, the function applies the binary operator- on the base iterator and returns a reverse iterator constructed with the resulting iterator value.

Note that this function requires the base iterator to be a random-access iterator.

This operator is also overloaded as a non-member function, with the left-hand and right-hand types inverted, but this the same behavior (see operator+).

Parameters

n
Number of elements to offset.
Member type difference_type is an alias of the base iterator's own difference type.

Return value

An iterator pointing to the element n positions away.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// reverse_iterator::operator+ example
#include <iostream>     // std::cout
#include <iterator>     // std::reverse_iterator
#include <vector>       // std::vector

int main () {
  std::vector<int> myvector;
  for (int i=0; i<10; i++) myvector.push_back(i);	// myvector: 0 1 2 3 4 5 6 7 8 9

  typedef std::vector<int>::iterator iter_type;

  std::reverse_iterator<iter_type> rev_it;

  rev_it = myvector.rbegin() +3;

  std::cout << "The fourth element from the end is: " << *rev_it << '\n';

  return 0;
}

Output:

The fourth element from the end is: 6


Data races

The object is accessed.
The iterator returned can be used to access or modify elements.

Exception safety

Provides the same level of guarantee as the operations internally applied to the base iterator.

See also