enumerate_view
is a range adaptor that takes a
view
and produces a view of
tuples.
i
th
element (the tuple) of the resulting sequence holds:
i
, which is a zero-based index of the element of underlying sequence, and3) 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.
V
base_
an iterator to the underlying view
enumerate_view
sized_range
approximately_sized_range
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range
and common_range
std::ranges::view_interface<D>
) [edit] returns the n
th element in the derived view, provided only if it satisfies random_access_range
std::ranges::view_interface<D>
) [edit] [edit] Deduction guides [edit] Nested classes the iterator type
This specialization of ranges::enable_borrowed_range makes enumerate_view
satisfy borrowed_range
when the underlying view satisfies it.
#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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4