Created
January 6, 2014 16:53
-
-
Save Fiona-J-W/8285744 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
template<unsigned Depth, typename Container, typename Function> | |
struct deep_for_each_helper { | |
static void deep_for_each(Container& cont, const Function& fun) { | |
for(auto& elem: cont) { | |
deep_for_each_helper<Depth-1, typename std::decay<decltype(elem)>::type, Function>::deep_for_each(elem, fun); | |
} | |
} | |
}; | |
template<typename Container, typename Function> | |
struct deep_for_each_helper<0, Container, Function> { | |
static void deep_for_each(Container& cont, const Function& fun) { | |
for(auto& elem: cont) { | |
fun(elem); | |
} | |
} | |
}; | |
template<unsigned Depth, typename Container, typename Function> | |
void deep_for_each(Container& cont, const Function& fun) { | |
deep_for_each_helper<Depth, Container, Function>::deep_for_each(cont, fun); | |
} | |
#include <iostream> | |
#include <vector> | |
#include <array> | |
int main() { | |
//std::array<std::vector<std::vector<int>>, 2> arr = {{{{1,2},{3,4}},{{4,5}}}}; | |
std::vector<std::vector<int>> arr = {{1,2}, {3}}; | |
deep_for_each<1>(arr, [](int i){ std::cout << i << '\n'; }); | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment