Skip to content

Commit 3960154

Browse files
TylerMSFTTylerMSFT
authored andcommitted
draft
1 parent 79ff785 commit 3960154

2 files changed

Lines changed: 88 additions & 5 deletions

File tree

docs/standard-library/ranges.md

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,104 @@ f1_keywords: ["<ranges>"]
66
helpviewer_keywords: ["ranges header"]
77
---
88

9+
introduction - what is a range
10+
motivations/advantages
11+
example
12+
range adaptors
13+
example
14+
view adaptors
15+
https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/ (good range/view examples, composability)
16+
example
17+
18+
919
# `<ranges>`
1020

21+
INTRO:
22+
23+
From eric niebler
24+
Ranges are an extension of the Standard Template Library that makes its iterators and algorithms more powerful by making them composable. Unlike other range-like solutions which seek to do away with iterators, in range-v3 ranges are an abstration layer on top of iterators.
25+
26+
Range-v3 is built on three pillars: Views, Actions, and Algorithms. The algorithms are the same as those with which you are already familiar in the STL, except that in range-v3 all the algorithms have overloads that take ranges in addition to the overloads that take iterators. Views are composable adaptations of ranges where the adaptation happens lazily as the view is iterated. And an action is an eager application of an algorithm to a container that mutates the container in-place and returns it for further processing.
27+
28+
Views and actions use the pipe syntax (e.g., rng | adapt1 | adapt2 | ...) so your code is terse and readable from left to right.
29+
30+
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4128.html#design-goals
31+
Ranges (not Iterables) are copyable and assignable. They don't own the elements like, say, a container
32+
33+
34+
> what it is - anything you can iterate through (docs.microsoft.com when talking about range-based for)
35+
Essentially, a range is something that can be traversed. More precisely, a range is something that has a begin() and an end() method, that return objects (iterators) that let you iterate over the range (that is, move along the elements of the range, and be dereferenced to access these elements). - https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/
36+
With ranges though, you generally don’t see iterators. They are here, but abstracted away by the concept of range. Iterators are technical constructs that let you iterate over a collection, but they are generally too technical for your functional code. Most of the time, what you are really trying to represent is a range, which corresponds better to the level of abstraction of your code. Like a range of cash flows, a range of lines in a screen, or a range of entries coming up from the database.
37+
38+
So coding in terms of ranges is a huge improvement, because in that sense iterators violate the principle of respecting levels of abstraction, which I deem is the most important principle for designing good code.
39+
40+
In range libraries, STL algorithms are redefined to take directly ranges as parameters, instead of two iterators, like:
41+
42+
ranges::transform(input, std::back_inserter(output), f);
43+
As opposed to:
44+
45+
std::transform(input.begin(), input.end(), std::back_inserter(output), f);
46+
Such algorithms reuse the STL versions in their implementation, by forwarding the begin and the end of the range to the native STL versions.
47+
48+
49+
> an example
50+
> the different types of ranges (input_range, output_range, etc. see https://www.modernescpp.com/index.php/c-20-the-ranges-library and search :input_range)
51+
> List of containers the range types can work with (same link as above)
52+
> What a View is: https://www.modernescpp.com/index.php/c-20-the-ranges-library
53+
A View is something that you apply on a range and performs some operation. A view does not own data and it's time to copy, move, assignment its constant.
54+
Lazy
55+
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
56+
auto results = numbers | std::views::filter([](int n){ return n % 2 == 0; })
57+
| std::views::transform([](int n){ return n * 2; });
58+
> What is a range adaptor:
59+
A range adaptor is an object that can be combined with a range in order to produce a new range.
60+
https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/
61+
With the following collection of numbers:
62+
63+
std::vector numbers = { 1, 2, 3, 4, 5 };
64+
The range
65+
66+
auto range = numbers | view::transform(multiplyBy2);
67+
is a view over the vector numbers that has the iteration behaviour of a transform_iterator with the function multiplyBy2. So when you iterate over this view, the results you get are all these numbers, multiplied by 2. For instance:
68+
69+
ranges::accumulate(numbers | view::transform(multiplyBy2), 0);
70+
returns 1*2 + 2*2 + 3*2 + 4*2 + 5*2 = 30 (similarly to std::accumulate, ranges::accumulate does the sum of the elements of the range it is passed to).
71+
72+
73+
74+
List of views: https://www.modernescpp.com/index.php/c-20-the-ranges-library
75+
76+
> What's a range adaptor: A range adaptor is an object that can be combined with a range in order to produce a new range. A subpart of them are view adaptors: with them, the initial adapted range remains unchanged, while the produced range does not contain elements because it is rather a view over the initial one, but with a customized iteration behaviour. See https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/
77+
> View adaptors: https://www.fluentcpp.com/2018/02/09/introduction-ranges-library/
78+
auto evenNumbers = numbers | ranges::view::filter([](int n){ return n % 2 == 0; }); Same as above?
79+
80+
We can plug as many view adaptors as we like. For example let me plug a transform adaptor with a function that multiplies a number by 2.
81+
82+
auto evenNumbers = numbers | ranges::view::filter([](int n){ return n % 2 == 0; })
83+
| ranges::view::transform([](int n) { return n * 2; });
84+
1185
A `range`, at its most basic, is a collection of things that you can iterate over.
1286

1387
The STL library contains many algorithms that take an iterator. You often pass a Begin(), End() iterator to define the items you want to process. That is flexible, but makes for a certain code wordiness. Rather than reverse a list, say, you have to pass a pair of iterators that define the boundaries of which parts of the list you want to reverse, and then pass the list along with that to a function to reverse them. Wouldn't it be nice if you could just pass the list and be done with it. Ranges allow this.
1488

89+
https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/
90+
91+
All algorithms manipulate iterators pointing into the collection they operate on. While this is handy in specific cases like stopping at a precise point in a container, the largely general case is to traverse the whole container from its .begin() to its .end().Therefore, portions of code that use the STL end up being littered with iterators:
92+
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
93+
std::set_difference(v2.begin(), v2.end(), v3.begin(), v3.end(), std::back_inserter(v4));
94+
std::transform(v3.begin(), v3.end(), std::back_inserter(v4));
95+
96+
Composability: https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/ Not sure I want to talk about this.
97+
1598
Saying that somehting is a range is essentially saying that it can be iterated over, which means that it has a begin, it has an end and they both return something that behaves essentially like an iterator. It’s a vague definition but they’re are a lot of things to fit into that definition and one of the is the STL containers, like std::vector for example.
1699

17100
You can think of a range as two iterators that refer to the beginning and end of a group of elements that you can iterate over. Because all containers support iterators, every container can be thought of as a range. Since all algorithms from Boost.Range expect a range as a first parameter, a container like std::vector can be passed directly. You don’t have to call begin() and end() and then pass two iterators separately. This protects you from mistakes such as passing the begin and end iterator in the wrong order or passing iterators that belong to two different containers. https://theboostcpplibraries.com/boost.range-algorithms
18101

19-
hanks to the ranges library in C++20, working with the Standard Template Library (STL) will become much more comfortable and powerful. The algorithms of the ranges library are lazy, can work directly on the container and can easily be composed. To make it short: The comfort and the power of the ranges library are due to its functional ideas. Let me show you what that means.
102+
thanks to the ranges library in C++20, working with the Standard Template Library (STL) will become much more comfortable and powerful. The algorithms of the ranges library are lazy, can work directly on the container and can easily be composed. To make it short: The comfort and the power of the ranges library are due to its functional ideas. Let me show you what that means.
20103

21104
I really like this intro to the types:
22105

23106
https://www.modernescpp.com/index.php/c-20-the-ranges-library
24-
25-
26107
https://www.modernescpp.com/index.php/c-20-the-ranges-librar
27108
// rangesFilterTransform.cpp
28109

@@ -149,3 +230,5 @@ lazy eval
149230
LINKS I LIKE
150231
--------------
151232
https://www.modernescpp.com/index.php/c-20-the-ranges-library
233+
https://www.fluentcpp.com/2018/02/09/introduction-ranges-library/
234+
https://www.fluentcpp.com/2017/01/12/ranges-stl-to-the-next-level/

docs/standard-library/span.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: "&lt;span&gt;"
2+
title: "<span>"
33
description: "API reference for the Standard Template Library (STL) span namespace, which provides a lightweight view over a contiguous sequence of objects."
44
ms.date: "05/28/2020"
55
f1_keywords: ["<span>"]
66
helpviewer_keywords: ["span header"]
77
---
88

9-
# &lt;span&gt;
9+
# `<span>`
1010

1111
A `span` is a view over a contiguous sequence of objects. It provides fast and bounds-safe access. Unlike `vector` or `array`, it doesn't "own" the elements it provides access to.
1212

0 commit comments

Comments
 (0)