|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Member functions | ||||
(C++26) | ||||
Iterator | ||||
Member functions | ||||
Non-member functions | ||||
Sentinel | ||||
Member functions | ||||
Non-member functions | ||||
Defined in header <ranges> | ||
template<ranges::forward_range V,std::size_t N> requiresranges::view<V>&&(N>0) | (1) | (since C++23) |
namespace views{ template<std::size_t N> | (2) | (since C++23) |
namespace views{ inlineconstexprauto pairwise= adjacent<2>; | (3) | (since C++23) |
Call signature | ||
template<ranges::viewable_range R> requires/* see below */ | (since C++23) | |
adjacent_view
is a range adaptor that takes aview
, and produces aview
whosei
th element (a “window”) is astd::tuple that holdsN
references to the elements[
i
,
i + N - 1
]
of the original view.S
be the size of the original view. Then the size of produced view is:S >= N
,forward_range
,adjacent_view
always modelsforward_range
, and modelsbidirectional_range
,random_access_range
, orsized_range
if adaptedview
type models the corresponding concept.
Contents |
Member | Description |
V base_ | the underlyingview (exposition-only member object*) |
constructs aadjacent_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 satisfiessized_range (public member function)[edit] | |
(C++26) | returns the approximate size of the resultingapproximately_sized_range (public member function)[edit] |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_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 only ifranges::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 satisfiesforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns then th element in the derived view, provided only if it satisfiesrandom_access_range (public member function of std::ranges::view_interface<D> )[edit] |
(none)
the iterator type (exposition-only member class template*) | |
the sentinel type used whenadjacent_view is not acommon_range (exposition-only member class template*) |
template<class V, size_t N> constexprboolranges::enable_borrowed_range<adjacent_view<V, N>>= | (since C++23) | |
This specialization ofranges::enable_borrowed_range makesadjacent_view
satisfyborrowed_range
when the underlying view satisfies it.
views::adjacent only accepts forward ranges even whenN
is0
.
There are similarities betweenranges::adjacent_view andranges::slide_view:
N
.S - N + 1
, whereS
is the size of an adaptedview
such thatS >= N > 0
.The following table shows the differences between these adaptors:
View adaptor | value_type | The window sizeN |
---|---|---|
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) | ranges::zip_view, ranges::zip_transform_view, ranges::adjacent_view ,ranges::adjacent_transform_view |
#include <array>#include <format>#include <iostream>#include <ranges>#include <tuple> int main(){constexprstd::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);}}
Output:
v = [1 2 3 4 5 6]e = [1 2 3]e = [2 3 4]e = [3 4 5]e = [4 5 6]
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 4098 | C++23 | views::adjacent<0> used to accept input-only ranges | made rejected |
aview consisting of results of application of a transformation function to adjacent elements of the adapted view(class template)(range adaptor object)[edit] | |
aview whose Mth element is aview over the Mth through (M + N - 1)th elements of anotherview (class template)(range adaptor object)[edit] | |
a range ofview s that areN -sized non-overlapping successive chunks of the elements of anotherview (class template)(range adaptor object)[edit] | |
aview consisting of elements of anotherview , advancing over N elements at a time(class template)(range adaptor object)[edit] |