|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
template<ranges::view V> requires/*range-with-movable-references*/<V> | (1) | (since C++23) |
namespace views{ inlineconstexpr/* unspecified */ enumerate=/* unspecified */; | (2) | (since C++23) |
Call signature | ||
template<ranges::viewable_range R> requires/* see below */ | (since C++23) | |
Helper concepts | ||
template<class R> concept/*range-with-movable-references*/= | (3) | (exposition only*) |
enumerate_view
is a range adaptor that takes aview
and produces a view oftuples.i
th element (the tuple) of the resulting sequence holds:i
, which is a zero-based index of the element of underlying sequence, andviews::enumerate
denotes aRangeAdaptorObject. Given a subexpressione, the expressionviews::enumerate(e) isexpression-equivalent toenumerate_view<views::all_t<decltype((e))>>(e) for any suitable subexpressione.enumerate_view
models the conceptsrandom_access_range
,bidirectional_range
,forward_range
,input_range
,common_range
, andsized_range
when the underlying viewV
models respective concepts.
Contents |
Member | Description |
V base_ | an iterator to the underlyingview (exposition-only member object*) |
constructs aenumerate_view (public member function)[edit] | |
returns a copy of the underlying (adapted) 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] |
(C++23) | the iterator type (exposition-only member class template*) |
(C++23) | the sentinel type (exposition-only member class template*) |
template<class View> constexprbool enable_borrowed_range<ranges::enumerate_view<View>>= | (since C++23) | |
This specialization ofranges::enable_borrowed_range makesenumerate_view
satisfyborrowed_range
when the underlying view satisfies it.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_enumerate | 202302L | (C++23) | std::ranges::enumerate_view |
#include <initializer_list>#include <iostream>#include <map>#include <ranges>#include <vector> int main(){constexprstaticauto v={'A','B','C','D'}; for(autoconst[index, letter]: std::views::enumerate(v))std::cout<<'('<< index<<':'<< letter<<") ";std::cout<<'\n'; #if __cpp_lib_ranges_to_container// create a map using the position of each element as keyauto m= v| std::views::enumerate| std::ranges::to<std::map>(); for(autoconst[key, value]: m)std::cout<<'['<< key<<"]:"<< value<<' ';std::cout<<'\n';#endif std::vector<int> numbers{1,3,5,7}; // num is mutable even with const, which does not propagate to reference to// make it const, use `std::views::enumerate(numbers) | std::views::as_const`// or `std::views::enumerate(std::as_const(numbers))`for(autoconst[index, num]: std::views::enumerate(numbers)){++num;// the type is int&std::cout<< numbers[index]<<' ';}std::cout<<'\n';}
Possible output:
(0:A) (1:B) (2:C) (3:D)[0]:A [1]:B [2]:C [3]:D2 4 6 8
(C++20) | aview consisting of a sequence generated by repeatedly incrementing an initial value(class template)(customization point object)[edit] |
(C++23) | aview consisting of tuples of references to corresponding elements of the adapted views(class template)(customization point object)[edit] |
takes aview consisting oftuple-like values and a number N and produces aview of Nth element of each tuple(class template)(range adaptor object)[edit] |