jack06215

[tools] python map()

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