In C++, is there a way to call a function on each element of a vector, without using a loop running over all vector elements? Something similar to a 'map' in Python.
Yes: std::for_each
.
#include <algorithm> //std::for_each void foo(int a) { std::cout << a << "\n"; } std::vector<int> v; ... std::for_each(v.begin(), v.end(), &foo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With