reverse_iterator::operator+
Adds an offset to an iterator and returns the new reverse_iterator addressing the inserted element at the new offset position.
reverse_iterator<RandomIterator> operator+(
difference_type _Off
) const;
Parameters
- _Off
The offset to be added to the reverse iterator.
Return Value
A reverse_iterator addressing the offset element.
Remarks
This member function may only be used if the reverse_iterator satisfies the requirements for a random-access iterator.
Example
// reverse_iterator_op_add.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
int i;
vector<int> vec;
for (i = 1 ; i < 6 ; ++i )
{
vec.push_back ( 2 * i );
}
vector <int>::iterator vIter;
cout << "The vector vec is: ( ";
for ( vIter = vec.begin( ) ; vIter != vec.end( ); vIter+)
cout << *vIter << " ";
cout << ")." << endl;
vector <int>::reverse_iterator rvIter;
cout << "The vector vec reversed is: ( ";
for ( rvIter = vec.rbegin( ) ; rvIter != vec.rend( ); rvIter+)
cout << *rvIter << " ";
cout << ")." << endl;
// Initializing reverse_iterators to the first element
vector <int>::reverse_iterator rVPOS1 = vec.rbegin ( );
cout << "The iterator rVPOS1 initially points to the first "
<< "element\n in the reversed sequence: "
<< *rVPOS1 << "." << endl;
vector <int>::reverse_iterator rVPOS2 =rVPOS1 + 2; // offset added
cout << "After the +2 offset, the iterator rVPOS2 points\n"
<< " to the 3rd element in the reversed sequence: "
<< *rVPOS2 << "." << endl;
}
The vector vec is: ( 2 4 6 8 10 ). The vector vec reversed is: ( 10 8 6 4 2 ). The iterator rVPOS1 initially points to the first element in the reversed sequence: 10. After the +2 offset, the iterator rVPOS2 points to the 3rd element in the reversed sequence: 6.
Requirements
Header: <iterator>
Namespace: std