|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Example demonstrating C++20 Ranges support in Aleph-w. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <cmath>#include <tclap/CmdLine.h>#include <htlist.H>#include <tpl_dynArray.H>#include <tpl_dynDlist.H>#include <ah-ranges.H>Go to the source code of this file.
Functions | |
| void | print_section (const string &title) |
| void | print_subsection (const string &title) |
| template<typename Container > | |
| void | print_container (const string &label, const Container &c) |
| void | demo_no_ranges () |
| int | main (int argc, char *argv[]) |
Example demonstrating C++20 Ranges support in Aleph-w.
This program demonstrates C++20 Ranges support in Aleph-w through ah-ranges.H. Ranges provide a modern, composable way to work with sequences of data using lazy evaluation and the pipe operator (|). This is the C++20 standard way of doing functional programming.
C++20 Ranges introduce:
range | filter | transform)Key advantage: No intermediate allocations until you "materialize" the range into a container.
Traditional approach (eager):
Ranges approach (lazy):
Transform ranges without creating new containers:
views::filter**: Keep elements satisfying predicateviews::transform**: Transform each elementviews::take**: Take first n elementsviews::drop**: Skip first n elementsviews::reverse**: Reverse orderViews are:
The pipe operator (|) allows natural composition:
Reads like: "Take data, filter positives, square them, take 10, convert to vector"
| Aspect | Traditional | C++20 Ranges |
|---|---|---|
| Evaluation | Eager (immediate) | Lazy (on demand) |
| Memory | Multiple allocations | Single allocation (at end) |
| Syntax | Nested calls | Pipe composition |
| Performance | Can be slower | Often faster (fewer allocations) |
-std=c++20 compiler flagOnly one allocation (at the end), not three intermediate allocations!
Definition in file ranges_example.C.
| void demo_no_ranges | ( | ) |
Definition at line 496 of file ranges_example.C.
References Aleph::maps(), and print_section().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 519 of file ranges_example.C.
References demo_no_ranges(), demo_performance(), demo_practical(), and Aleph::maps().
Definition at line 173 of file ranges_example.C.
References Aleph::maps().
| void print_section | ( | const string & | title | ) |
Definition at line 160 of file ranges_example.C.
References Aleph::maps().
Referenced by demo_no_ranges().
| void print_subsection | ( | const string & | title | ) |
Definition at line 167 of file ranges_example.C.
References Aleph::maps().