std::ranges::views::enumerate, std::ranges::enumerate_view
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Ranges library
returns the approximate size of the resulting
(public member function) [edit]
returns a constant iterator to the beginning of the range
(public member function of
returns a sentinel for the constant iterator of the range
(public member function of
a
(class template) (customization point object)[edit]
a
(class template) (customization point object)[edit]
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++26)(C++26)
(C++23)(C++23)
enumerate_viewviews::enumerate
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
std::ranges::enumerate_view
(C++26)
Defined in header
<ranges>
template< ranges::view V >
(1)
(since C++23)
requires /*range-with-movable-references*/<V>
class enumerate_view
namespace views {
(2)
(since C++23)
inline constexpr /* unspecified */ enumerate = /* unspecified */;
Call signature
template< ranges::viewable_range R >
(since C++23)
requires /* see below */
Helper concepts
template< class R >
(3)
(exposition only*)
concept /*range-with-movable-references*/ =
ranges::input_range <R> &&
std::move_constructible <ranges::range_reference_t <R>> &&
1)
enumerate_view
is a range adaptor that takes a view
and produces a view of tuple s. i
th 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 RangeAdaptorObject. Given a subexpression e, the expression views::enumerate(e) is expression-equivalent 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 random_access_range
, bidirectional_range
, forward_range
, input_range
, common_range
, and sized_range
when the underlying view V
models respective concepts.
Contents
[edit] Data members
Member
Description
[edit] Member functions
returns the number of elements, provided only if the underlying (adapted) range satisfies
(public member function) [edit]
sized_range
(public member function) [edit]
(C++26)
approximately_sized_range
(public member function) [edit]
Inherited from std::ranges::view_interface
returns whether the derived view is empty, provided only if it satisfies
(public member function of
sized_range
or forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
(C++23)
(public member function of
std::ranges::view_interface<D>
) [edit]
(C++23)
(public member function of
std::ranges::view_interface<D>
) [edit]
returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
(public member function of
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the first element in the derived view, provided if it satisfies
(public member function of
forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the last element in the derived view, provided only if it satisfies
(public member function of
bidirectional_range
and common_range
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the
(public member function of
n
th element in the derived view, provided only if it satisfies random_access_range
(public member function of
std::ranges::view_interface<D>
) [edit]
[edit] Deduction guides
[edit] Nested classes
[edit] Helper templates
template< class View >
(since C++23)
constexpr bool enable_borrowed_range<ranges::enumerate_view<View>> =
This specialization of ranges::enable_borrowed_range makes enumerate_view
satisfy borrowed_range
when the underlying view satisfies it.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_enumerate |
202302L |
(C++23) | std::ranges::enumerate_view
|
[edit] Example
Run this code
#include <initializer_list> #include <iostream> #include <map> #include <ranges> #include <vector> int main() { constexpr static auto v = {'A', 'B', 'C', 'D'}; for (auto const [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 key auto m = v | std::views::enumerate | std::ranges::to <std::map >(); for (auto const [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 (auto const [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
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 26.7.23 Enumerate view [range.enumerate]
[edit] See also
(C++20)
view
consisting of a sequence generated by repeatedly incrementing an initial value(class template) (customization point object)[edit]
(C++23)
view
consisting of tuples of references to corresponding elements of the adapted views(class template) (customization point object)[edit]
takes a
(class template) (range adaptor object)[edit]
view
consisting of tuple-like values and a number N and produces a view
of Nth element of each tuple(class template) (range adaptor object)[edit]