I'm a little late too; but you can use this (C-style variational function):
template<typename T> void foreach(std::function<void(T)> callback, int count...) { va_list args; va_start(args, count); for (int i = 0; i < count; i++) { std::vector<T> v = va_arg(args, std::vector<T>); std::for_each(v.begin(), v.end(), callback); } va_end(args); } foreach<int>([](const int &i) {
or this (using the function parameter package):
template<typename Func, typename T> void foreach(Func callback, std::vector<T> &v) { std::for_each(v.begin(), v.end(), callback); } template<typename Func, typename T, typename... Args> void foreach(Func callback, std::vector<T> &v, Args... args) { std::for_each(v.begin(), v.end(), callback); return foreach(callback, args...); } foreach([](const int &i){
or this (using parenthesized list of initializers):
template<typename Func, typename T> void foreach(Func callback, std::initializer_list<std::vector<T>> list) { for (auto &vec : list) { std::for_each(vec.begin(), vec.end(), callback); } } foreach([](const int &i){
or you can join vectors, like here: What is the best way to concatenate two vectors? and then iterate over the big vector.
Szymon Marczak Jul 28 '17 at 11:43 2017-07-28 11:43
source share