24 Ranges library [ranges]

24.7 Range adaptors [range.adaptors]

24.7.15 Elements view [range.elements]

24.7.15.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.
The name views​::​elements<N> denotes a range adaptor object ([range.adaptor.object]).
Given a subexpression E and constant expression N, the expression views​::​elements<N>(E) is expression-equivalent to elements_­view<views​::​all_­t<decltype((E))>, N>{E}.
Example
:
auto historical_figures = map{
  {"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 << ' ';          // prints Babbage Hamilton Lovelace Turing 
}

auto birth_years = historical_figures | views::elements<1>;
for (auto&& born : birth_years) {
  cout << born << ' ';          // prints 1791 1936 1815 1912 
}
— end example
 ]
keys_­view is an alias for elements_­view<views​::​all_­t<R>, 0>, and is useful for extracting keys from associative containers.
Example
:
auto names = keys_view{historical_figures};
for (auto&& name : names) {
  cout << name << ' ';          // prints Babbage Hamilton Lovelace Turing 
}
— end example
 ]
values_­view is an alias for elements_­view<views​::​all_­t<R>, 1>, and is useful for extracting values from associative containers.
Example
:
auto is_even = [](const auto x) { return x % 2 == 0; };
cout << ranges::count_if(values_view{historical_figures}, is_even);     // prints 2
— end example
 ]