fork(2) download
  1. #include <map>
  2. #include <functional>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using ::std::string;
  7.  
  8. template<typename Key, typename Value, template <typename, typename, typename, typename> class Map>
  9. struct ForEachOf {
  10. void operator()(const Map<Key, Value, ::std::less<Key>, ::std::allocator<std::pair<const Key, Value>>>& map, std::function<void (Key, Value)> func) {
  11. for(const auto& pair : map) {
  12. func(pair.first, pair.second);
  13. }
  14. }
  15. };
  16.  
  17. int main(void) {
  18. std::map<int, string> m { {1, "foo"}, {3, "bar"}};
  19. ForEachOf<int, string, std::map> forEachOf;
  20. forEachOf(m, [](int key, string value) {
  21. ::std::cout << key << value;
  22. });
  23. }
  24.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1foo3bar