You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Range adapters create a *view* (one of the [view classes](view-classes.md) in the `std::views` namespace) from a range. We recommend that you use an adapter in `std::ranges::views` instead of creating the view types directly. The adapters are the intended way to access views. They're easier to use, and in some cases more efficient, than creating instances of the view types directly.
11
11
12
-
A *view* is a lightweight object that refers to elements from a range. A view can:
12
+
A view is a lightweight object that refers to elements from a range. A view can:
13
13
14
14
- Consist of only certain elements from a range.
15
15
- Represent a transformation of elements from a range.
@@ -83,9 +83,9 @@ Range adapters come in many forms. For example, there are range adapters that al
83
83
- Transforming the elements in a range (`view::transform`).
84
84
- Splitting a range (`view::split`).
85
85
86
-
Range adapters can be chained together (composed). That's where the power and flexibility of ranges is most apparent. Composing range adapters allows you to overcome a core problem with the previous Standard Template Library (STL) algorithms, which is that they aren't easy to chain together.
86
+
Range adapters can be chained together (composed). That's where the power and flexibility of ranges are most apparent. Composing range adapters allows you to overcome a core problem with the previous Standard Template Library (STL) algorithms: that they aren't easy to chain together.
87
87
88
-
The following range adapters are available in the `std::views` namespace. The `std::views`namespace is a convenience alias for `std::ranges::views`.
88
+
The following range adapters are available in the `std::views` namespace. The `std::views`namespace is a convenience alias for `std::ranges::views`.
89
89
90
90
|**Range adapter**|**Description**|
91
91
|--|--|
@@ -115,7 +115,7 @@ In the previous table, a range adapter is typically described as taking a range
115
115
116
116
Range adapter functions are typically [function objects](https://eel.is/c++draft/function.objects), which look like function calls and enforce constraints on the types that can be passed.
117
117
118
-
You can pass range adapters and the result of pipe operations (`|`) to code that expects function objects. In the following example, the view created by the `split` range adapter is passed to the `transform` range adapter as if by a function call, because the `transform` range adapter is a function object.
118
+
You can pass range adapters and the result of pipe operations (`|`) to code that expects function objects. In the following example, the view that the `split` range adapter creates is passed to the `transform` range adapter as if by a function call, because the `transform` range adapter is a function object.
119
119
120
120
```cpp
121
121
std::map<int, string> x = {{0, "Hello, world"}, {42, "Goodbye, world"}};
@@ -144,18 +144,18 @@ The range to create the view from.
144
144
### Return value
145
145
146
146
- If `rg` is already a view, a copy of `rg`.
147
-
- If `rg` is a non-view lvalue, a [`ref_view`](ref-view-class.md) that refers to `rg`. (The lifetime of the view is tied to the lifetime of `rg`.)
147
+
- If `rg` is a non-view `lvalue`, a [`ref_view`](ref-view-class.md) that refers to `rg`. (The lifetime of the view is tied to the lifetime of `rg`.)
148
148
- If `rg` is a non-view `rvalue` such as a temporary object, or is the result of passing the range to `std::move`, an [`owning_view`](owning-view-class.md).
149
149
150
150
Use `std::views::all_t<decltype((rg))>` to get the type of the returned view.
151
151
152
152
### Remarks
153
153
154
-
This range adapter is the best way to convert a range into a view. One reason to create a view from a range is to pass it by value at low cost if passing the range by value could be expensive.
154
+
This range adapter is the best way to convert a range into a view. One reason to create a view from a range is to pass it by value at low cost, if passing the range by value could be expensive.
155
155
156
156
Getting a view for a range is a useful alternative to passing a heavyweight range by value because views are inexpensive to create, copy, and destroy. A possible exception is `owning_view`, which is a view that owns the underlying range.
157
157
158
-
In general, the worstcase scenario for destroying a view has `O(N)` complexity for the number of elements in the range. Even if you destroy `K` copies of view with `N` elements, the total complexity is still `O(N)` because the underlying range is destroyed only once.
158
+
In general, the worst-case scenario for destroying a view has `O(N)` complexity for the number of elements in the range. Even if you destroy `K` copies of view with `N` elements, the total complexity is still `O(N)` because the underlying range is destroyed only once.
159
159
160
160
### Example: `all`
161
161
@@ -344,12 +344,12 @@ If you specify more elements to drop than exist in the underlying range, an [`em
344
344
345
345
The returned view is typically, but not always, a specialization of [`drop_view`](drop-view-class.md). That is:
346
346
347
-
- If `V` is a specialization of [`empty_view`](empty-view-class.md) or a specialization of [`span`](span-class.md), [`basic_string_view`](basic-string-view-class.md), [`iota_view`](iota-view-class.md), or [`subrange`](subrange-class.md) that is both `random_access_range` and `sized_range`, the result is a specialization of `V`.
347
+
- If `V` is a specialization of [`empty_view`](empty-view-class.md), or is a specialization of [`span`](span-class.md), [`basic_string_view`](basic-string-view-class.md), [`iota_view`](iota-view-class.md), or [`subrange`](subrange-class.md) that is both `random_access_range` and `sized_range`, the result is a specialization of `V`.
348
348
- Otherwise, the result is a [`drop_view`](drop-view-class.md).
349
349
350
350
### Remarks
351
351
352
-
After it's created, the number of elements in the view stays the same even if the view it was created from changes. However, if the underlying view changes, accessing elements in the returned view might result in undefined behavior.
352
+
After it's created, the number of elements in the view stays the same even if the view that it was created from changes. However, if the underlying view changes, accessing elements in the returned view might result in undefined behavior.
353
353
354
354
`drop` is the opposite of [`take`](#take).
355
355
@@ -1198,7 +1198,7 @@ The number of elements to take from the front of `rg`.
1198
1198
1199
1199
The returned view is typically, but not always, a specialization of [`take_view`](take-view-class.md). Specifically:
1200
1200
1201
-
- If `V` is a specialization of [`empty_view`](empty-view-class.md) or a specialization of [`span`](span-class.md), [`basic_string_view`](basic-string-view-class.md), [`iota_view`](iota-view-class.md), or [`subrange`](subrange-class.md) that is both `random_access_range` and `sized_range`, the result is a specialization of `V`.
1201
+
- If `V` is a specialization of [`empty_view`](empty-view-class.md), or is a specialization of [`span`](span-class.md), [`basic_string_view`](basic-string-view-class.md), [`iota_view`](iota-view-class.md), or [`subrange`](subrange-class.md) that is both `random_access_range` and `sized_range`, the result is a specialization of `V`.
1202
1202
- Otherwise, the result is a [`take_view`](take-view-class.md).
1203
1203
1204
1204
### Remarks
@@ -1347,7 +1347,7 @@ A [`transform_view`](transform-view-class.md) that contains the transformed elem
1347
1347
1348
1348
For efficiency's sake, when you compose `filter` and `transform`, do the `filter` first so that you `transform` only the elements that you intend to keep.
1349
1349
1350
-
The code shown earlier as "2\)" can be used with pipe syntax: `collection | transform(fun)`. Or you can use it with function call syntax: `transform(collection, fun)` or `transform(fun)(collection)`.
1350
+
The code shown earlier as "2\)" can be used with pipe syntax: `collection | transform(fun)`. Or it can be used with function call syntax: `transform(collection, fun)` or `transform(fun)(collection)`.
0 commit comments