Section hub

std::ranges::views::adjacent, std::ranges::adjacent_view, std::ranges::views::pairwise

  1. adjacent_view is a range adaptor that takes a view, and produces a view whose ith element (a “window”) is a std::tuple that holds N references to the elements of the original view, from ith up to i + N - 1th inclusively.

# Declarations

template< ranges::forward_range V, std::size_t N >
requires ranges::view<V> && (N > 0)
class adjacent_view
: public ranges::view_interface<adjacent_view<V, N>>

(since C++23)

namespace views {
template< std::size_t N >
constexpr /* unspecified */ adjacent = /* unspecified */ ;
}

(since C++23)

namespace views {
inline constexpr auto pairwise = adjacent<2>;
}

(since C++23)

Call signature
template< ranges::viewable_range R >
requires /* see below */
constexpr ranges::view auto adjacent<N>( R&& r );

(since C++23)

# Notes

views::adjacent only accepts forward ranges even when N is 0.

There are similarities between ranges::adjacent_view and ranges::slide_view:

The following table shows the differences between these adaptors:

# Example

#include <array>
#include <format>
#include <iostream>
#include <ranges>
#include <tuple>
 
int main()
{
    constexpr std::array v{1, 2, 3, 4, 5, 6};
    std::cout << "v = [1 2 3 4 5 6]\n";
 
    for (int i{}; std::tuple t : v | std::views::adjacent<3>)
    {
        auto [t0, t1, t2] = t;
        std::cout << std::format("e = {:<{}}[{} {} {}]\n", "", 2 * i++, t0, t1, t2);
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 4098C++23views::adjacent<0> used to accept input-only rangesmade rejected

# See also

This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.

Core adapters

Start here for the adapters most people reach for first when building pipelines.

Utility views

These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.

New in C++23 / C++26

Newer adapters, kept as a compact scan list with only standard badges.

All entities by category

A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.

74 entities