Section hub

std::ranges::views::enumerate, std::ranges::enumerate_view

  1. enumerate_view is a range adaptor that takes a view and produces a view of tuples. 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, andthe reference to the underlying element.

# Declarations

template< ranges::view V >
requires /*range-with-movable-references*/<V>
class enumerate_view
: public ranges::view_interface<enumerate_view<V>>

(since C++23)

namespace views {
inline constexpr /* unspecified */ enumerate = /* unspecified */;
}

(since C++23)

Call signature
template< ranges::viewable_range R >
requires /* see below */
constexpr /* see below */ enumerate( R&& r );

(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>>;

(exposition only*)

# Notes

Feature-test macro Value Std Feature __cpp_lib_ranges_enumerate 202302L (C++23) std::ranges::enumerate_view

# Example

#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';
}

# See also

This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.

Core adapters

Start here for the adapters most people reach for first when building pipelines.

Utility views

These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.

New in C++23 / C++26

Newer adapters, kept as a compact scan list with only standard badges.

All entities by category

A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.

74 entities