avr39ripe

cppLambdaExpr

Sep 29th, 2021
708
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <memory>
  3. #include <array>
  4. #include <vector>
  5. #include <forward_list>
  6. #include <algorithm>
  7. #include <functional>
  8.  
  9. namespace itstep
  10. {
  11.     template <typename InIter, typename Func>
  12.     void for_each(InIter begin, InIter end, Func func)
  13.     {
  14.         for (; begin != end; ++begin)
  15.         {
  16.             func(*begin);
  17.         }
  18.     }
  19.  
  20.     template <typename T>
  21.     void print(const T& elem, char delimeter = '\0')
  22.     {
  23.         std::cout << elem;
  24.         if (delimeter)
  25.         {
  26.             std::cout << delimeter;
  27.         }
  28.     }
  29.  
  30.     template <typename T>
  31.     void mul2(T& elem)
  32.     {
  33.         elem *= 2;
  34.     }
  35.  
  36. }
  37.  
  38. //void tester(void(*func)(int, int))
  39. //template<typename FuncType>
  40. //void tester(FuncType func)
  41. void tester(std::function<void(int, int)> func)
  42. {
  43.     func(rand() % 10, rand() % 10);
  44. }
  45.  
  46. void print3Num(int a, int b, int c)
  47. {
  48.     std::cout << "a = " << a << " b = " << b << " c = " << c << '\n';
  49. }
  50.  
  51. void wrapper(int a, int c) { print3Num(a, 55, c); }
  52.  
  53. int main()
  54. {
  55.     std::vector<int> dynArr{ 1,2,3,4,5,6,7,8,9,10 };
  56.  
  57.     //itstep::print(10, '\t');
  58.     //itstep::print(20, ' ');
  59.     //itstep::print(30);
  60.  
  61.     std::for_each(dynArr.begin(), dynArr.end(), [](auto el) {itstep::print<int>(el, '\t'); });
  62.  
  63.     std::for_each(dynArr.begin(), dynArr.end(), [](auto el) {itstep::print<int>(el, ' '); });
  64.     //const int fixedVal{ 55 };
  65.  
  66.     ////tester(wrapper);
  67.     //tester([fixedVal](int first, int second)->void { print3Num(first, fixedVal, second); });
  68.     //tester([fixedVal](int first, int second)->void { print3Num(fixedVal, first, second); });
  69.     //return 0;
  70.     //using intFunct = int(*)(int, int);
  71.  
  72.     //auto maxL{ [](int a, int b) {return a > b ? a : b; } };
  73.  
  74.     //int x{ 42 };
  75.     //int y{ 33 };
  76.     //int z{ 77 };
  77.  
  78.     //auto numberPrinter{ [x,&y]()mutable -> void { std::cout << x++ << ' ' << y++ << '\n'; return ; } };
  79.     //
  80.     //// [&x,y,&z](){}
  81.     //auto empty{ [z](int num, char sym, bool flag){ } };
  82.     //
  83.  
  84.     //numberPrinter();
  85.  
  86.     //x = 442;
  87.     //y = 333;
  88.  
  89.     //numberPrinter();
  90.  
  91.     //std::cout << "x = " << x << '\n';
  92.  
  93.     ////int(*fPtr)(int, int);
  94.     //intFunct fPtr;
  95.  
  96.     //std::function<int(int, int)> funcPtr{ maxL };
  97.     //fPtr = maxL;
  98.  
  99.     //auto copyMax{ maxL };
  100.  
  101.     //std::cout << funcPtr(10, 20) << '\n';
  102.  
  103.     //std::array<int, 10> staticArr;
  104.     ////std::vector<int> dynArr{1,2,3,4,5,6,7,8,9,10};
  105.  
  106.     //std::forward_list<int> dynArr{ 1,2,3,4,5,6,7,8,9,10 };
  107.  
  108.     //auto begin{ dynArr.begin() };
  109.     //auto end{ dynArr.end() };
  110.  
  111.     //auto printer{ [](auto& elem) { std::cout << elem << ' '; } };
  112.  
  113.     //itstep::for_each(dynArr.begin(), dynArr.end(), printer);
  114.     //std::cout << '\n';
  115.     //std::for_each(dynArr.begin(), dynArr.end(), [](auto& elem) { elem *= 2; });
  116.     //std::cout << '\n';
  117.     //std::for_each(dynArr.begin(), dynArr.end(), printer);
  118.  
  119.     // [] () {}
  120.     return 0;
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
RAW Paste Data