From cppreference.com
Defined in header
template<ranges::viewV>requires/*range-with-movable-references*/<V>classenumerate_view:publicranges::view_interface<enumerate_view<V>> (1) (since C++23)namespaceviews{inlineconstexpr/* unspecified */enumerate=/* unspecified */;} (2) (since C++23)Call signature
template<ranges::viewable_rangeR>requires/* see below */constexpr/* see below */enumerate(R&&r);(since C++23)Helper concepts
template<classR>concept/*range-with-movable-references*/=ranges::input_range<R>&&std::move_constructible<ranges::range_reference_t<R>>&&std::move_constructible<ranges::range_rvalue_reference_t<R>>; (3) (exposition only*)1)enumerate_view is a range adaptor that takes a
and produces a view of
s. ith element (the tuple) of the resulting sequence holds:
the value equal to i, which is a zero-based index of the element of underlying sequence, and
the reference to the underlying element.
2) The name views::enumerate denotes a
. Given a subexpression e, the expression views::enumerate(e) is
to enumerate_view<views::all_t<decltype((e))>>(e) for any suitable subexpression e.
3) Ensures that the reference type of the underlying type can be moved.
enumerate_view models the concepts
,
,
,
, and
when the underlying view V models respective concepts. It models
when V models
,
, and
.
Data members
Member Description Vbase_an iterator to the underlying
(exposition-only member object*)Member functions
constructs a enumerate_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
returns the number of elements, provided only if the underlying (adapted) range satisfies
(public member function)
(C++26)
returns the approximate size of the resulting
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
Nested classes
(C++23)
the iterator type
(exposition-only member class template*)(C++23)
the sentinel type
(exposition-only member class template*)Helper templates
template<classView>constexprboolenable_borrowed_range<ranges::enumerate_view<View>>=ranges::enable_borrowed_range<View>;(since C++23)This specialization of
makes enumerate_view satisfy
when the underlying view satisfies it.
Notes
macroValueStdFeature
(C++23)std::ranges::enumerate_viewExample
Run this code
#include<initializer_list>#include<iostream>#include<map>#include<ranges>#include<vector>intmain(){constexprstaticautov={'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 keyautom=v|std::views::enumerate|std::ranges::to<std::map>();for(autoconst[key,value]:m)std::cout<<'['<<key<<"]:"<<value<<' ';std::cout<<'\n';#endifstd::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]:D 2 4 6 8 References
C++23 standard (ISO/IEC 14882:2024):
26.7.23 Enumerate view [range.enumerate]
See also