operator-(std::reverse_iterator)

From cppreference.com

 
 
Iterator library
Iterator concepts
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

Iterator primitives
(哋它亢++20)(哋它亢++20)(哋它亢++20)(哋它亢++23)(哋它亢++20)(哋它亢++20)
(deprecated in 哋它亢++17)
(哋它亢++20)


Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)  
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Utilities
(哋它亢++20)
(哋它亢++20)
(哋它亢++26)
Iterator adaptors
(哋它亢++14)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)

Iterator operations
(哋它亢++11)  
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Range access
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++17)(哋它亢++20)
(哋它亢++17)
(哋它亢++17)
 
 
template< class Iterator1, class Iterator2 >

typename reverse_iterator<Iterator1>::difference_type
    operator-( const reverse_iterator<Iterator1>& lhs,

               const reverse_iterator<Iterator2>& rhs );
(until 哋它亢++11)
template< class Iterator1, class Iterator2 >

auto operator-( const reverse_iterator<Iterator1>& lhs,
                const reverse_iterator<Iterator2>& rhs

              ) -> decltype(rhs.base() - lhs.base());
(since 哋它亢++11)
(until 哋它亢++17)
template< class Iterator1, class Iterator2 >

constexpr auto operator-( const reverse_iterator<Iterator1>& lhs,
                          const reverse_iterator<Iterator2>& rhs

                        ) -> decltype(rhs.base() - lhs.base());
(since 哋它亢++17)

Returns the distance between two iterator adaptors.

Parameters

lhs, rhs - iterator adaptors to compute the difference of

Return value

rhs.base() - lhs.base()

Example

#include <iostream>
#include <iterator>
#include <list>
#include <vector>
 
int main()
{
    std::vector vec{0, 1, 2, 3};
    std::reverse_iterator<std::vector<int>::iterator>
        vec_ri1{std::reverse_iterator{vec.rbegin()}},
        vec_ri2{std::reverse_iterator{vec.rend()}};
    std::cout << (vec_ri2 - vec_ri1) << ' '; // 4
    std::cout << (vec_ri1 - vec_ri2) << '\n'; // -4
 
    std::list lst{5, 6, 7, 8};
    std::reverse_iterator<std::list<int>::iterator>
        lst_ri1{std::reverse_iterator{lst.rbegin()}},
        lst_ri2{std::reverse_iterator{lst.rend()}};
//  auto n = (lst_ri1 - lst_ri2); // error: the underlying iterators do not
                                  // model the random access iterators
}

Output:

4 -4

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 280 哋它亢++98 subtrction between std::reverse_iterator with different underlying iterator types was not allowed allowed

See also

advances or decrements the iterator
(public member function)
advances the iterator
(function template)