fork download
  1. #include <iostream>
  2.  
  3. #include <iostream>
  4. #include <map>
  5.  
  6. template<class Iter, class F>
  7. void apply( Iter begin, Iter end, F f, bool forward )
  8. {
  9. while( begin != end )
  10. f( forward ? *begin++ : *--end );
  11. }
  12.  
  13. int main()
  14. {
  15. const bool descend = false;
  16.  
  17. std::map<int, int> mapp;
  18. mapp[1] = 1;
  19. mapp[2] = 2;
  20. mapp[3] = 3;
  21. mapp[4] = 4;
  22.  
  23. auto print = []( const auto &p ) { std::cout << p.first << " "; };
  24. apply( mapp.begin(), mapp.end(), print, true );
  25. std::cout << std::endl;
  26. apply( mapp.begin(), mapp.end(), print, false );
  27. std::cout << std::endl;
  28. }
  29.  
  30.  
Success #stdin #stdout 0s 4332KB
stdin
Standard input is empty
stdout
1 2 3 4 
4 3 2 1