jack06215

[STL] for_each()

Jul 11th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <functional>
  6. #include <iterator>
  7.  
  8.  
  9.  
  10. template<typename Collection, typename unop>
  11. void for_each(Collection col, unop op) {
  12.     std::for_each(col.begin(), col.end(), op);
  13. }
  14.  
  15. int main()
  16. {
  17.     auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
  18.     auto lambda_echo = [](int i) { std::cout << std::setw(3) << i; };
  19.     std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
  20.  
  21.     // call for_each to print the vector
  22.     println("running for_each<T>");
  23.     for_each(col, lambda_echo);
  24.  
  25.     return 0;
  26. }
Add Comment
Please, Sign In to add comment