C/C++ Arena

Step 5 of 6

Ranges

C++20 ranges let you pass a container directly to algorithms and build lazy pipelines with |:

#include <ranges>
#include <algorithm>

std::ranges::sort(v);        // no begin/end needed

for (int x : v | std::views::filter([](int x) { return x % 2 == 0; })
               | std::views::transform([](int x) { return x * x; })) {
    ...   // squares of the even numbers, computed on demand
}

Your turn: write std::vector<std::string> top_fraggers(const std::vector<std::pair<std::string, int>>& players, int min_kills) that returns the names of players with at least min_kills, in their original order, using views::filter and views::transform.

Previous: Concepts Next: std::span