Commit fda73d52 authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Modify C++20 range example to highlight ordering, as advised by Vincent.

parent cef327b8
......@@ -605,7 +605,7 @@
"\n",
"It is also important to highlight that while the STL algorithms may provide you efficiency (this library is written by highly skilled engineers after all), this is not its main draw: the algorithms are written to be as generic as possible. The primary reason to use them is to allow you to think at a higher level of abstraction, not to get the fastest possible implementation. So if your ~~intuition~~ benchmarking has shown that the standard library is causing a critical slowdown, you are free to explore classic alternatives such as [loop unrolling](https://en.wikipedia.org/wiki/Loop_unrolling) - that's one of the strength of the language (and the STL itself opens up this possibility directly for some of its construct - you may for instance use your own memory allocator when defining a container). For most purposes however that will not be necessary.\n",
"\n",
"FYI, C++ 20 introduces a completely new way to deal with algorithms, which does not rely on direct use of iterators but instead on a range library. This leads to a syntax which is more akin to what is done in other languages (see [@ Coliru](https://coliru.stacked-crooked.com/a/23b256ac459633ff)):\n"
"FYI, C++ 20 introduces a completely new way to deal with algorithms, which does not rely on direct use of iterators but instead on a range library. This leads to a syntax which is more akin to what is done in other languages (see [@Coliru](https://coliru.stacked-crooked.com/a/0a49f5fa446fbeb9)):\n"
]
},
{
......@@ -625,11 +625,30 @@
"\n",
" std::vector<int> pi_digits = { 3, 1, 4, 1, 5, 9};\n",
" \n",
" auto results = pi_digits | std::views::filter([](int n){ return n % 2 != 0; })\n",
" | std::views::reverse;\n",
" {\n",
" auto filter_then_transform = pi_digits | std::views::filter([](int n){ return n % 2 != 0; })\n",
" | std::views::transform([](int n) { return n == 1 ? 0 : n; });\n",
" \n",
" std::cout << \"Filter then transform : \"; \n",
" \n",
" for (auto v: results) \n",
" std::cout << v << \" \"; // 9 5 1 1 3\n",
" for (auto v: filter_then_transform) \n",
" std::cout << v << \" \"; // 3 0 0 5 9\n",
" }\n",
" \n",
" std::cout << std::endl << \"Initial vector unchanged : \";\n",
" for (auto v: pi_digits) \n",
" std::cout << v << \" \"; // 3 1 4 1 5 9\n",
" std::cout << std::endl;\n",
" \n",
" {\n",
" auto transform_then_filter = pi_digits | std::views::transform([](int n) { return n == 1 ? 0 : n; })\n",
" | std::views::filter([](int n){ return n % 2 != 0; });\n",
" \n",
" std::cout << \"Transform then filter : \"; \n",
" \n",
" for (auto v: transform_then_filter) \n",
" std::cout << v << \" \"; // 3 5 9\n",
" } \n",
"\n",
" return EXIT_SUCCESS;\n",
"}"
......@@ -639,6 +658,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As seen in this example, the reading order follows the same convention as Unix shell: operations are done from the left to the right.\n",
"\n",
"Having no first hand experience of it I really can't say more about it but don't be astonished if you meet such a syntax in a C++ program."
]
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment