Function Composition
Learn function composition in C++20 along with examples.
We'll cover the following...
In the example below, I use a std::map
, because the ordering of the keys is crucial.
Press + to interact
#include <iostream>#include <ranges>#include <string>#include <map>int main() {std::map<std::string, int> freqWord{ {"witch", 25}, {"wizard", 33},{"tale", 45}, {"dog", 4},{"cat", 34}, {"fish", 23} };std::cout << "All words: ";for (const auto& name : std::views::keys(freqWord)) { std::cout << name << " "; }std::cout << '\n';std::cout << "All words, reversed: ";for (const auto& name : std::views::keys(freqWord)| std::views::reverse) { std::cout << name << " "; }std::cout << '\n';std::cout << "The first 4 words: ";for (const auto& name : std::views::keys(freqWord)| std::views::take(4)) { std::cout << name << " "; }std::cout << '\n';std::cout << "All words starting with w: ";auto firstw = [](const std::string& name){ return name[0] == 'w'; };for (const auto& name : std::views::keys(freqWord)| std::views::filter(firstw)) { std::cout << name << " "; }std::cout << '\n';}
I’m only interested in the keys. I display all of them ( ...