C++ Boost Adaptor
Code for using filtered / transformed view over some existing containers
This is similar to range-v3, a feature that will be in c++20.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
auto main() -> int {
// the program should print the cubed of even number only
auto v = std::vector<int>{2, 1, 3, 5, 4};
auto power_of_three = [](auto num) { return num * num * num; };
for (auto elem : v
| boost::adaptors::filtered([](auto num) { return num % 2 == 0; })
| boost::adaptors::transformed(power_of_three)) {
std::cout << elem << std::endl;
}
return 0;
}
// g++ -o a.out -std=c++17 -I/path/to/boost test.cpp && ./a.out
// should have output:
8
64
This post is licensed under CC BY 4.0 by the author.