Advertisement
Gistrec

for_each

May 30th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. template <typename Type>
  2. void for_each(const Type& container, std::function<void(typename Type::value_type)> &funct) {
  3.     for (auto&& elem : container) {
  4.         funct(elem);
  5.     }
  6. }
  7.  
  8. int main() {
  9.     vector<int> v = { 1, 2, 3 };
  10.     for_each(v, [](int e){
  11.         cout << e << endl;
  12.     });
  13.  
  14.     for_each(vector<int>({ 1, 2, 3 }), [](int e){
  15.         cout << e << endl;
  16.     });
  17.  
  18.     function<void(int)> funct = [](int e) {
  19.         cout << e << endl;
  20.     };
  21.  
  22.     for_each(v, funct);
  23.  
  24.     for_each(vector<int>{1}, funct);
  25.  
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement