(no title)
omegaham | 3 years ago
#include <ranges>
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3};
// map, using `std::views::transform`. Implemented in C++20
for(int elem :
std::views::transform(vec, [](int x) { return x + 1; })) {
std::cout << elem << " "; // 2 3 4
}
std::cout << "\n";
// `std::ranges::fold_left` will be available with C++23.
// Until then, we're stuck using the <numeric> iterator version, which was
// implemented in C++20.
std::cout << std::accumulate(vec.begin(), vec.end(), 1,
[](int x, int y) { return x * y; })
<< "\n"; // 6
return 0;
}
Building and running with the following: $ g++ --std=c++20 test.cpp
$ ./a.out
2 3 4
6
delta_p_delta_x|3 years ago