Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | ranges
Revision as of 05:18, 29 March 2024 by BenFrantzDale (Talk | contribs)

 
 
Ranges library
Range adaptors
adjacent_viewviews::adjacent
(C++23)(C++23)
views::pairwise
(C++23)
 
 
Defined in header <ranges>
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>>
(1) (since C++23)
namespace views {

template< std::size_t N >
    inline constexpr /* unspecified */ adjacent = /* unspecified */ ;

}
(2) (since C++23)
namespace views {

    inline constexpr auto pairwise = adjacent<2>;

}
(3) (since C++23)
Call signature
template< ranges::viewable_range R >

    requires /* see below */

constexpr ranges::view auto adjacent<N>( R&& r );
(since C++23)
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.
Let S be the size of the original view. Then the size of produced view is:
  • S - N + 1, if S >= N,
  • 0 otherwise, and the resulting view is empty.
2) The name views::adjacent<N> denotes a RangeAdaptorObject. Given a subexpression e and a constant expression N, the expression views::adjacent<N>(e) is expression-equivalent to
  • ((void)e, auto(views::empty<tuple<>>)) if N is equal to 0,
  • adjacent_view<views::all_t<decltype((e))>, N>(e) otherwise.
3) The name views::pairwise denotes a RangeAdaptorObject that behaves exactly as views::adjacent<2>.

adjacent_view always models forward_range, and models bidirectional_range, random_access_range, or sized_range if adapted view type models the corresponding concept.

Contents

Data members

Member object Definition
base_ (private) the underlying view of type V.
(exposition-only member object*)

Member functions

constructs a adjacent_view
(public member function) [edit]
returns an iterator to the beginning
(public member function) [edit]
returns an iterator or a sentinel to the end
(public member function) [edit]
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range.
(public member function) [edit]
Inherited from std::ranges::view_interface
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a constant iterator to the beginning of the range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a sentinel for the constant iterator of the range.
(public member function of std::ranges::view_interface<D>) [edit]
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>) [edit]
returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>) [edit]
returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

Deduction guides

(none)

Nested classes

(C++23)
the iterator type
(exposition-only member class template*)
(C++23)
the sentinel type used when adjacent_view is not a common_range
(exposition-only member class template*)

Helper templates

template< class V, size_t N >

inline constexpr bool ranges::enable_borrowed_range<adjacent_view<V, N>> =

    ranges::enable_borrowed_range<V>;
(since C++23)

This specialization of ranges::enable_borrowed_range makes adjacent_view satisfy borrowed_range when the underlying view satisfies it.

Notes

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

  • Both create a "sliding window" of size N.
  • Both have the same size S - N + 1, where S is the size of an adapted view such that S >= N > 0.

The following table shows the differences between these adaptors:

View adaptor value_type The window size N
ranges::adjacent_view std::tuple A template parameter
ranges::slide_view ranges::range A runtime argument
Feature-test macro Value Std Feature
__cpp_lib_ranges_zip 202110L (C++23) std::ranges::zip_view,

std::ranges::zip_transform_view,
std::ranges::adjacent_view,
std::ranges::adjacent_transform_view

Example

std::views::adjacent<3>:\n"; for (int i{}; std::tuple t : v

#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";
 
    std::cout << "\nv
[{} {} {}]\n", "", 2 * i++, t0, t1, t2);
   }
   std::cout << "\nv | std::views::adjacent<5>:\n";
   for (int i{}; std::tuple t : v | std::views::adjacent<5>)
   {
       auto [t0, t1, t2, t3, t4] = t;
       std::cout << std::format("e = {:<{}}[{} {} {} {} {}]\n", "", 2 * i++, t0, t1, t2, t3, t4);
   }
   std::cout << "\nv | std::views::adjacent<6>:\n";
   for (int i{}; std::tuple t : v | std::views::adjacent<6>)
   {
       auto [t0, t1, t2, t3, t4, t5] = t;
       std::cout << std::format("e = {:<{}}[{} {} {} {} {} {}]\n", "", 2 * i++, t0, t1, t2, t3, t4, t5);
   }
   std::cout << "\nv | std::views::adjacent<7>:\n";
   for (int i{}; std::tuple t : v | std::views::adjacent<7>)
   {
       // Note: This code will never execute because v.size() < 7
       // Note: Even then, the type of the items has the expected static size of 7:
       static_assert(std::tuple_size_v<decltype(t)> == 7);
       auto [t0, t1, t2, t3, t4, t5, t6] = t;
       std::cout << std::format("e = {:<{}}[{} {} {} {} {} {} {}]\n", "", 2 * i++, t0, t1, t2, t3, t4, t5, t6);
   }

} |output= v = [1 2 3 4 5 6]

v | std::views::adjacent<3>: e = [1 2 3] e = [2 3 4] e = [3 4 5] e = [4 5 6]

v | std::views::adjacent<5>: e = [1 2 3 4 5] e = [2 3 4 5 6]

v | std::views::adjacent<6>: e = [1 2 3 4 5 6]

v | std::views::adjacent<7>:

}}

References

  • C++23 standard (ISO/IEC 14882:2024):
  • 26.7.25 Adjacent view [range.adjacent]

See also

a view consisting of results of application of a transformation function to adjacent elements of the adapted view
(class template) (range adaptor object)[edit]
a view whose Mth element is a view over the Mth through (M + N - 1)th elements of another view
(class template) (range adaptor object)[edit]
a range of views that are N-sized non-overlapping successive chunks of the elements of another view
(class template) (range adaptor object)[edit]
a view consisting of elements of another view, advancing over N elements at a time
(class template) (range adaptor object)[edit]