Advertisement
avr39ripe

cppSTLExamples

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