From Jason Turner

[range.elements.overview]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp7s4lwlxm/{from.md → to.md} +61 -0
tmp/tmp7s4lwlxm/{from.md → to.md} RENAMED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### Overview <a id="range.elements.overview">[[range.elements.overview]]</a>
2
+
3
+ `elements_view` takes a `view` of tuple-like values and a `size_t`, and
4
+ produces a `view` with a value-type of the Nᵗʰ element of the adapted
5
+ `view`’s value-type.
6
+
7
+ The name `views::elements<N>` denotes a range adaptor object
8
+ [[range.adaptor.object]]. Given a subexpression `E` and constant
9
+ expression `N`, the expression `views::elements<N>(E)` is
10
+ expression-equivalent to
11
+ `elements_view<views::all_t<decltype((E))>, N>{E}`.
12
+
13
+ [*Example 1*:
14
+
15
+ ``` cpp
16
+ auto historical_figures = map{
17
+ {"Lovelace"sv, 1815},
18
+ {"Turing"sv, 1912},
19
+ {"Babbage"sv, 1791},
20
+ {"Hamilton"sv, 1936}
21
+ };
22
+
23
+ auto names = historical_figures | views::elements<0>;
24
+ for (auto&& name : names) {
25
+ cout << name << ' '; // prints Babbage Hamilton Lovelace Turing
26
+ }
27
+
28
+ auto birth_years = historical_figures | views::elements<1>;
29
+ for (auto&& born : birth_years) {
30
+ cout << born << ' '; // prints 1791 1936 1815 1912
31
+ }
32
+ ```
33
+
34
+ — *end example*]
35
+
36
+ `keys_view` is an alias for `elements_view<views::all_t<R>, 0>`, and is
37
+ useful for extracting keys from associative containers.
38
+
39
+ [*Example 2*:
40
+
41
+ ``` cpp
42
+ auto names = keys_view{historical_figures};
43
+ for (auto&& name : names) {
44
+ cout << name << ' '; // prints Babbage Hamilton Lovelace Turing
45
+ }
46
+ ```
47
+
48
+ — *end example*]
49
+
50
+ `values_view` is an alias for `elements_view<views::all_t<R>, 1>`, and
51
+ is useful for extracting values from associative containers.
52
+
53
+ [*Example 3*:
54
+
55
+ ``` cpp
56
+ auto is_even = [](const auto x) { return x % 2 == 0; };
57
+ cout << ranges::count_if(values_view{historical_figures}, is_even); // prints 2
58
+ ```
59
+
60
+ — *end example*]
61
+