A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reverse_iterator::operator- below:

public member function

<iterator>

std::reverse_iterator::operator-
reverse_iterator operator- (difference_type n) const;

Subtraction operator

Returns a reverse iterator pointing to the element located n positions before 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 to return the difference of subtracting iterators: see operator-).


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 An iterator pointing to the element n positions before the currently pointed one.

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_iterator;

  rev_iterator = myvector.rend() - 3;

  std::cout << "myvector.rend()-3 points to: " << *rev_iterator << '\n';

  return 0;
}

Output:
myvector.rend()-3 points to: 2


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
reverse_iterator::operator+
Addition operator (public member function)
reverse_iterator::operator--
Decrease iterator position (public member function)
reverse_iterator::operator-=
Retrocede iterator (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4