Namespaces
Variants
Views
Actions

operator-(std::reverse_iterator)

From cppreference.com
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Utilities
(C++20)
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
(C++11)
(C++11)
Range access
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 
 
template< class Iterator >

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

               const reverse_iterator<Iterator>& rhs );
(until C++11)
template< class Iterator1, class Iterator2 >

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

              ) -> decltype(rhs.base() - lhs.base());
(since C++11)
(until C++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 C++17)

Returns the distance between two iterator adaptors.

Contents

[edit] Parameters

lhs, rhs - iterator adaptors to compute the difference of

[edit] Return value

rhs.base() - lhs.base()

[edit] Example

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

Output:

4 -4

[edit] See also

advances or decrements the iterator
(public member function) [edit]
advances the iterator
(function template) [edit]