January 2018
Intermediate to advanced
374 pages
9h 53m
English
The ranges library consists of three types of operations: algorithms, actions, and views. It's important to know the difference between views and actions, the following code snippet shows what a simple procedure corresponds to when using actions versus using views. Note how the actions mutate the vector, whereas the view simply iterates it:
// Prerequisiteauto is_odd = [](int v){ return (v % 2) == 1; }auto square = [](int v){ return v*v; }auto get() { return std::vector<int>{1,2,3,4}; }using ra = ranges::action;using rv = ranges::view;
// Print odd squares using actions...for(auto v: get() | ra::remove_if(is_odd) | ra::transform(square)) { std::cout << v << " "; }// ... corresponds to the following code where ...