Namespaces
Variants
Views
Actions

std::ranges::adjacent_view<V,N>::iterator<Const>::operator++,--,+=,-=

From cppreference.com
< cpp‎ | ranges‎ | adjacent view‎ | iterator
 
 
Ranges library
Range access
Range conversions
(C++23)
Range primitives



Dangling iterator handling
Range concepts
Views

Factories




Adaptors
Range generators
Range adaptor objects
Range adaptor closure objects
Helper items
 
std::ranges::adjacent_view
Member functions
Iterator
Member functions
adjacent_view::iterator::operator++
adjacent_view::iterator::operator++(int)
adjacent_view::iterator::operator--
adjacent_view::iterator::operator--(int)
adjacent_view::iterator::operator+=
adjacent_view::iterator::operator-=
(C++23)(C++23)(C++23)(C++23)(C++23)(C++23)
Non-member functions
Sentinel
Member functions
Non-member functions
 
constexpr /*iterator*/& operator++();
(1) (since C++23)
constexpr /*iterator*/ operator++( int );
(2) (since C++23)
constexpr /*iterator*/& operator--()
  requires ranges::bidirectional_range<Base>;
(3) (since C++23)
constexpr /*iterator*/ operator--( int )
  requires ranges::bidirectional_range<Base>;
(4) (since C++23)
constexpr /*iterator*/& operator+=( difference_type x )
  requires ranges::random_access_range<Base>;
(5) (since C++23)
constexpr /*iterator*/& operator-=( difference_type x )
  requires ranges::random_access_range<Base>;
(6) (since C++23)

Increments or decrements the iterator.

Let current_ be an underlying array of iterators.

1) Equivalent to:
for (auto& i : current_)
    std::ranges::next(i);
return *this;
The behavior is undefined if before the call the current_.back() is not incrementable.
2) Equivalent to:
auto tmp = *this; ++*this; return tmp;
3) Equivalent to:
for (auto& i : current_)
    std::ranges::prev(i);
return *this;
The behavior is undefined if before the call the current_.front() is not decrementable.
4) Equivalent to:
auto tmp = *this; --*this; return tmp;
5) Equivalent to:
for (auto& i : current_)
    i += x;
return *this;
The behavior is undefined if before the call the current_.back() + x does not have well-defined behavior.
6) Equivalent to:
for (auto& i : current_)
    i -= x;
return *this;
The behavior is undefined if before the call the current_.front() - x does not have well-defined behavior.

Contents

[edit] Parameters

n - position relative to current location

[edit] Return value

1,3,5,6) *this
2,4) a copy of *this that was made before the change

[edit] Example

A link to test: Compiler Explorer/g++-13

#include <cassert>
#include <list>
#include <ranges>
#include <utility>
#include <vector>
 
[[nodiscard]]
bool operator== (std::pair<int&, int&> x, std::pair<int, int> y)
{
    return x.first == y.first and x.second == y.second;
}
 
int main()
{
    {
        auto v = std::vector{0, 1, 2, 3, 4, 5};
        auto i = (v | std::views::pairwise).begin();
        assert((*i == std::pair{0, 1}));
        ++i;                            // overload (1)
        assert((*i == std::pair{1, 2}));
        --i;                            // overload (3)
        assert((*i == std::pair{0, 1}));
        i += 2;                         // overload (5)
        assert((*i == std::pair{2, 3}));
        i -= 2;                         // overload (6)
        assert((*i == std::pair{0, 1}));
    }
    {
        auto v = std::list{0, 1, 2, 3, 4, 5};
        auto i = (v | std::views::pairwise).begin();
        assert((*i == std::pair{0, 1}));
        ++i;                            // overload (1)
        assert((*i == std::pair{1, 2}));
        --i;                            // overload (3)
        assert((*i == std::pair{0, 1}));
//      i += 2; // Error: v is not a random_access_range; overload (5)
//      i -= 2; // Error: v is not a random_access_range; overload (6)
    }
}

[edit] See also

performs iterator arithmetic
(public member function)