std::ranges...umerate_view(3)
β¨ std::ranges...umerate_view(3) β¨
okay so bestie, welcome to the C++ Standard Libary manual page for std::ranges...umerate_view(3) β yes the title is doing the absolute most, and we are gonna get through every single detail together. π«Ά
πΈ NAME πΈ
std::ranges::views::enumerate,std::ranges::enumerate_viewβ literally the two names this page is about. The NAME line repeats them exactly:std::ranges::views::enumerate,std::ranges::enumerate_view. No cap, that's the whole name section. π
π¦ Synopsis π¦
Defined in header <ranges>:
template< ranges::view V >
requires /*range-with-movable-references*/<V>
class enumerate_view
: public ranges::view_interface<enumerate_view<V>>
That's number (1), since C++23. β¨
namespace views {
inline constexpr /* unspecified */ enumerate = /* unspecified */;
}
That's number (2), since C++23. π
π Call signature π
template< ranges::viewable_range R >
requires /* see below */
constexpr /* see below */ enumerate( R&& r );
That's since C++23. π¦
π§ Helper concepts π§
template< class R >
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>>;
That's number (3), and it's exposition only*. It ensures that the reference type of the underlying type can be moved. π₯
enumerate_viewis a range adaptor that takes a view and produces a view of tuples. Thei^thelement (the tuple) of the resulting sequence holds:- the value equal to
i, which is a zero-based index of the element of the underlying sequence, and - the reference to the underlying element.
- the value equal to
- The name
views::enumeratedenotes aRangeAdaptorObject. Given a subexpressione, the expressionviews::enumerate(e)is expression-equivalent toenumerate_view<views::all_t<decltype((e))>>(e)for any suitable subexpressione. - 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. π
π Member functions π
- constructor β constructs a
enumerate_view. (C++23) (public member function) - base β returns a copy of the underlying (adapted) view. (C++23) (public member function)
- begin β returns an iterator to the beginning. (C++23) (public member function)
- end β returns an iterator or a sentinel to the end. (C++23) (public member function)
- size β returns the number of elements. Provided only if the underlying (adapted) range satisfies
sized_range. (C++23) (public member function)
π Inherited from std::ranges::view_interface π
emptyβ returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (C++20) (public member function ofstd::ranges::view_interface<D>)cbeginβ returns a constant iterator to the beginning of the range. (C++23) (public member function ofstd::ranges::view_interface<D>)cendβ returns a sentinel for the constant iterator of the range. (C++23) (public member function ofstd::ranges::view_interface<D>)operator boolβ returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (C++20) (public member function ofstd::ranges::view_interface<D>)frontβ returns the first element in the derived view. Provided if it satisfiesforward_range. (C++20) (public member function ofstd::ranges::view_interface<D>)backβ returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (C++20) (public member function ofstd::ranges::view_interface<D>)operator[]β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range. (C++20) (public member function ofstd::ranges::view_interface<D>)
π Deduction guides π
Bestie, the page says Deduction guides and then gives no extra details. That's it. Don't invent, okay? π
π Nested classes π
iteratorβ the iterator type.(C++23)(exposition-only member class template*)sentinelβ the sentinel type.(C++23)(exposition-only member class template*)
π§ Helper templates π§
template< class View >
constexpr bool enable_borrowed_range<ranges::enumerate_view<View>> =
std::ranges::enable_borrowed_range<View>;
That's since C++23. πΈ
This specialization of std::ranges::enable_borrowed_range makes enumerate_view satisfy borrowed_range when the underlying view satisfies it. β¨
π Notes π
- Feature-test macro:
__cpp_lib_ranges_enumerate - Value:
202302L - Std:
(C++23) - Feature:
std::ranges::enumerate_view
π Example π
Run this code π
#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
π References π
- C++23 standard (
ISO/IEC 14882:2023):26.7.23 Enumerate view [range.enumerate]
π See also π
ranges::iota_view/views::iotaβ a view consisting of a sequence generated by repeatedly incrementing an initial value.(C++20)(class template) (customization point object)ranges::zip_view/views::zipβ a view consisting of tuples of references to corresponding elements of the adapted views.(C++23)(class template) (customization point object)ranges::elements_view/views::elementsβ takes a view consisting of tuple-like values and a numberNand produces a view ofN^thelement of each tuple.(C++20)(class template) (range adaptor object)
mwah π that's the whole page, bestie. Sign-off source/version/date energy: http://cppreference.com, 2024.06.10, std::ranges...umerate_view(3). Byeee π¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms