Suppose we have a function:
int square(int x) { return x * x; }
How can we apply it to a vector (or list)?
STL provides a function transform
to achieve this:
vector<int> dst(src.size());
transform(src.begin(), src.end(), dst.begin(), square);
In this repo, I constructed a function template, which could make this easier:
auto dst = square%(src);
See more details in demo.cpp, and try following:
$ g++ -Wall -std=c++17 demo.cpp
$ ./a.out
vector : 1 3 5 7 9
transform: 1 9 25 49 81
square% : 1 9 25 49 81
list : 1 3 5 7 9
transform: 1 9 25 49 81
square% : 1 9 25 49 81