kennen:include:ranges
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
kennen:include:ranges [2024-02-20 15:22] – [<ranges>] rrichter | kennen:include:ranges [2024-02-22 21:04] (aktuell) – [Beispiele] rrichter | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== < | ====== < | ||
Bereiche sind Elementfolgen, | Bereiche sind Elementfolgen, | ||
- | Die Algorithmen der Standardbibliothek erlauben | + | Die Algorithmen der Standardbibliothek erlauben |
Allerdings sind die einzelnen Algorithmen jeweils nacheinander auszuführen, | Allerdings sind die einzelnen Algorithmen jeweils nacheinander auszuführen, | ||
+ | MIt einem anderen Ansatz soll dieser Nachteil behoben werden. | ||
Im Namensraum '' | Im Namensraum '' | ||
Zeile 125: | Zeile 126: | ||
===== Beispiele ===== | ===== Beispiele ===== | ||
+ | < | ||
+ | empty< | ||
+ | single(42) | ||
+ | iota(0, | ||
+ | iota(0) | take(3) | ||
+ | repeat(6) | take(3) | ||
+ | counted(begin(v), | ||
+ | v | all : [1, 3, 5, 4, 2] | ||
+ | v | drop_while(below_5) | ||
+ | v | enumerate | ||
+ | v | filter(below_5) | ||
+ | v | reverse | ||
+ | v | take_while(below_5) | ||
+ | v | transform(square) | ||
+ | |||
+ | v | chunk(3) | ||
+ | v | chunk_by(std:: | ||
+ | v | pairwise_transform(double_digit) | ||
+ | v | pairwise | ||
+ | v | slide(2) | ||
+ | v | stride(2) | ||
+ | |||
+ | s : [' | ||
+ | s | split(' | ||
+ | s | split(' | ||
+ | s | split(' | ||
+ | |||
+ | zip(v, s) : [(1, ' | ||
+ | zip_transform(double_digit, | ||
+ | cartesian_product(v|take(2), | ||
+ | |||
+ | m : {" | ||
+ | m | keys : [" | ||
+ | m | values | ||
+ | </ | ||
+ | ist die Ausgabe des Programms | ||
<code cpp views_examples.cpp> | <code cpp views_examples.cpp> | ||
#include < | #include < | ||
- | #include < | ||
#include <map> | #include <map> | ||
#include < | #include < | ||
#include < | #include < | ||
- | // waiting for P2286 Formatting Ranges ... | + | // waiting for C++23 support: |
+ | #if defined(__cpp_lib_format_ranges) && defined(__cpp_lib_print) | ||
+ | #include < | ||
template < | template < | ||
void show(Range&& | void show(Range&& | ||
{ | { | ||
- | std:: | + | |
+ | } | ||
+ | |||
+ | template < | ||
+ | void show_range_of_ranges(Range&& | ||
+ | { | ||
+ | show(r, message); | ||
+ | } | ||
+ | |||
+ | template < | ||
+ | void show_range_of_pairs(Range&& | ||
+ | { | ||
+ | show(r, message); | ||
+ | } | ||
+ | |||
+ | template < | ||
+ | void show_map(Range&& | ||
+ | { | ||
+ | show(r, message); | ||
+ | } | ||
+ | |||
+ | #else | ||
+ | #include < | ||
+ | |||
+ | auto decorate(auto e) | ||
+ | { | ||
+ | return e; | ||
+ | } | ||
+ | |||
+ | auto decorate(char e) | ||
+ | { | ||
+ | return std:: | ||
+ | } | ||
+ | |||
+ | auto decorate(std:: | ||
+ | { | ||
+ | return std:: | ||
+ | } | ||
+ | |||
+ | template < | ||
+ | void show(Range&& | ||
+ | { | ||
+ | | ||
std::cout << std::left << message << | std::cout << std::left << message << | ||
int cnt = 0; | int cnt = 0; | ||
for (auto&& | for (auto&& | ||
{ | { | ||
- | std::cout << (cnt++ ? ",":"" | + | std::cout << (cnt++ ? ", ":"" |
- | std:: | + | |
} | } | ||
std::cout << " | std::cout << " | ||
Zeile 152: | Zeile 231: | ||
void show_range_of_ranges(Range&& | void show_range_of_ranges(Range&& | ||
{ | { | ||
- | std:: | + | std:: |
std::cout << std::left << message << " : ["; | std::cout << std::left << message << " : ["; | ||
int cnt = 0; | int cnt = 0; | ||
for (auto&& | for (auto&& | ||
{ | { | ||
- | std::cout << (cnt++ ? ",":"" | + | std::cout << (cnt++ ? ", ":"" |
int inner_cnt = 0; | int inner_cnt = 0; | ||
for (auto&& | for (auto&& | ||
{ | { | ||
- | std::cout << (inner_cnt++ ? ",":"" | + | std::cout << (inner_cnt++ ? ", ":"" |
- | | + | << |
} | } | ||
std::cout << " | std::cout << " | ||
Zeile 168: | Zeile 247: | ||
std::cout << " | std::cout << " | ||
} | } | ||
+ | |||
+ | template < | ||
+ | void show_range_of_pairs(Range&& | ||
+ | { | ||
+ | std:: | ||
+ | std::cout << std::left << message << | ||
+ | int cnt = 0; | ||
+ | for (auto&& | ||
+ | { | ||
+ | std::cout << (cnt++ ? ", ":"" | ||
+ | << " | ||
+ | } | ||
+ | std::cout << " | ||
+ | } | ||
+ | |||
+ | template < | ||
+ | void show_map(Range&& | ||
+ | { | ||
+ | std:: | ||
+ | std::cout << std::left << message << | ||
+ | int cnt = 0; | ||
+ | for (auto&& | ||
+ | { | ||
+ | std::cout << (cnt++ ? ", ":"" | ||
+ | << decorate(k) << ": " << decorate(v); | ||
+ | } | ||
+ | std::cout << " | ||
+ | } | ||
+ | |||
+ | #endif // defined(__cpp_lib_format_ranges) && defined(__cpp_lib_print) | ||
int main() | int main() | ||
{ | { | ||
using namespace std::views; | using namespace std::views; | ||
+ | using namespace std:: | ||
| | ||
- | auto v = std:: | + | auto v = std:: |
- | auto s = std:: | + | auto s = " |
auto m = std:: | auto m = std:: | ||
+ | auto below_5 = [](int x){ return x < 5; }; | ||
+ | auto double_digit = [](int a, int b) { return 10*a + b; }; | ||
| | ||
show( empty< | show( empty< | ||
Zeile 181: | Zeile 293: | ||
show( iota(0,6), " | show( iota(0,6), " | ||
show( iota(0) | take(3), " | show( iota(0) | take(3), " | ||
- | show( counted(begin(v), | + | |
+ | std::cout << ' | ||
+ | |||
+ | | ||
show( v | all, "v | all"); | show( v | all, "v | all"); | ||
+ | show( v | drop_while(below_5), | ||
+ | show_range_of_pairs(v | enumerate, "v | enumerate" | ||
+ | show( v | filter(below_5), | ||
show( v | reverse, "v | reverse" | show( v | reverse, "v | reverse" | ||
- | show( v | take_while([](int x){ return x < 3; }), "v | take_while(below_3)" | + | show( v | take_while(below_5), "v | take_while(below_5)"); |
- | show( v | drop_while([](int x){ return x < 3; }), "v | drop_while(below_3)" | + | |
- | show( v | filter([](int x){ return x%2 != 0; }), "v | filter(odd)"); | + | |
show( v | transform([](int x){ return x*x; }), "v | transform(square)" | show( v | transform([](int x){ return x*x; }), "v | transform(square)" | ||
- | | + | |
+ | |||
+ | show_range_of_ranges( v | chunk(3), "v | chunk(3)" | ||
+ | show_range_of_ranges( v | chunk_by(std:: | ||
+ | show( v | pairwise_transform(double_digit), | ||
+ | show_range_of_pairs( v | pairwise, "v | pairwise" | ||
+ | show_range_of_ranges( v | slide(2), "v | slide(2)" | ||
+ | show( v | stride(2), "v | stride(2)" | ||
+ | std::cout << ' | ||
show( s , " | show( s , " | ||
show_range_of_ranges( s | split(' | show_range_of_ranges( s | split(' | ||
show( s | split(' | show( s | split(' | ||
- | | + | |
+ | std::cout << ' | ||
+ | |||
+ | show_range_of_pairs( zip(v, s), " | ||
+ | show( zip_transform(double_digit, | ||
+ | show_range_of_pairs( cartesian_product(v|take(2), | ||
+ | std::cout << ' | ||
+ | |||
+ | show_map( m , " | ||
show( m | keys , "m | keys" | show( m | keys , "m | keys" | ||
show( m | values , "m | values" | show( m | values , "m | values" | ||
} | } | ||
</ | </ | ||
- | erzeugt die Ausgabe | + | |
- | < | + | |
- | empty< | + | |
- | single(42) | + | |
- | iota(0, | + | |
- | iota(0) | take(3) | + | |
- | counted(begin(v), | + | |
- | v | all : [0, | + | |
- | v | reverse | + | |
- | v | take_while(below_3) | + | |
- | v | drop_while(below_3) | + | |
- | v | filter(odd) | + | |
- | v | transform(square) | + | |
- | s : [A,B, ,C,D, ,E] | + | |
- | s | split(' | + | |
- | s | split(' | + | |
- | m | keys : [answer, | + | |
- | m | values | + | |
- | </ | + |
kennen/include/ranges.1708438966.txt.gz · Zuletzt geändert: 2024-02-20 15:22 von rrichter