Post

C++ Ranges

Code for std ranges to transform from a collection to another collection after some ranges operations, available in c++23. Doing this we can have AAA style.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
#include <string>
#include <ranges>
#include <map>

int main() {
    auto src = std::map<std::string, int>{};
    auto dst = std::ranges::to<std::vector<std::string>>(
            src |
            std::views::filter([](const auto& p) { return p.second == 0; }) |
            std::views::transform([](const auto& p) { return p.first; }));
    return 0;
}

https://godbolt.org/z/vMn1se8P7

This post is licensed under CC BY 4.0 by the author.

Trending Tags