lib/viewspipe.cpp

The following code example is taken from the book
C++20 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2021
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <iostream>
#include <vector>
#include <map>
#include <ranges>

int main()
{
  namespace vws = std::views;

  // map of composers names and their years of birth:
  std::map<std::string, int> composers{
    {"Bach", 1685},
    {"Mozart", 1756},
    {"Beethoven", 1770},
    {"Tchaikovsky", 1840},
    {"Chopin", 1810},
    {"Vivaldi ", 1678},
  };

  // iterate over the names of the first 3 composers born since 1700:
  for (const auto& elem : composers
                           | vws::filter([](const auto& y) {   // since 1700
                                           return y.second >= 1700;
                                         })
                           | vws::take(3)                      // first 3
                           | vws::keys                         // keys/names only
                           ) {
    std::cout << "- " << elem << '\n';
  }
}