|
operator-
public member function
reverse_iterator operator- (difference_type n) const;
Subtraction operator
Returns a reverse iterator pointing to n elements before the element the object points to.
A global overload for operator- also exists for the case where one reverse_iterator object is subtracted from another one:
1 2 3 4
|
template <class Iterator>
typename reverse_iterator<Iterator>::difference_type operator- (
const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y);
|
In this case, the function returns the difference between the two iterators (i.e., the number of elements between the elements pointed).
Parameters
- n
- Number of elements to offset.
difference_type is a member type defined as an alias of the base iterator's own difference type (generally, an integral type).
Return value
The reverse iterator itself (*this).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// reverse_iterator::operator- example
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main () {
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 vector<int>::iterator iter_int;
reverse_iterator<iter_int> rev_iterator;
rev_iterator = myvector.rend() - 3;
cout << "myvector.rend() points to : " << *rev_iterator << endl;
return 0;
}
|
Output:
myvector.rend() points to: 2
|
See also
operator+ | Addition operator (public member function) |
operator-- | Decrease iterator position (public member function) |
operator-= | Retrocede iterator (public member function) |
|