[range.elements.overview] (original) (raw)

25 Ranges library [ranges]

25.7 Range adaptors [range.adaptors]

25.7.23 Elements view [range.elements]

25.7.23.1 Overview [range.elements.overview]

elements_view takes a view of tuple-like values and a size_t, and produces a view with a value-type of the element of the adapted view's value-type.

Given a subexpression E and constant expression N, the expression views​::​elements<N>(E) is expression-equivalent toelements_view<views​::​all_t<decltype((E))>, N>{E}.

[Example 1: auto historical_figures = map{ pair{"Lovelace"sv, 1815},{"Turing"sv, 1912},{"Babbage"sv, 1791},{"Hamilton"sv, 1936} };auto names = historical_figures | views::elements<0>;for (auto&& name : names) { cout << name << ' '; } auto birth_years = historical_figures | views::elements<1>;for (auto&& born : birth_years) { cout << born << ' '; } — _end example_]

keys_view is an alias for elements_view<R, 0>, and is useful for extracting keys from associative containers.

[Example 2: auto names = historical_figures | views::keys;for (auto&& name : names) { cout << name << ' '; } — _end example_]

values_view is an alias for elements_view<R, 1>, and is useful for extracting values from associative containers.

[Example 3: auto is_even = [](const auto x) { return x % 2 == 0; }; cout << ranges::count_if(historical_figures | views::values, is_even); — _end example_]