avr39ripe

PV913FunctionPtrFunctorsExplained

Aug 19th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5.  
  6. template <typename ElemType>
  7. class Printer
  8. {
  9.     std::ostream& out;
  10.     char delimiter;
  11. public:
  12.     Printer(std::ostream& outP, char delimiterP) : out{ outP }, delimiter{ delimiterP } {};
  13.     Printer() :Printer{ std::cout, '\0' } {};
  14.     Printer(char delimiterP) : Printer{ std::cout, delimiterP } {};
  15.     void operator()(const ElemType& elem) const
  16.     {
  17.         out << elem;
  18.         if (delimiter) out << delimiter;
  19.     };
  20. };
  21.  
  22. class Numerator
  23. {
  24.     int curr;
  25.     int delta;
  26.     static int sumCnt;
  27. public:
  28.     Numerator(int startP, int deltaP) : curr{ startP }, delta{ deltaP }{};
  29.     Numerator() : Numerator{ 0,1 } {};
  30.     int operator()()
  31.     {
  32.         ++sumCnt;
  33.         int ret{ curr };
  34.         curr += delta;
  35.         return ret;
  36.     }
  37.     static int getSumCnt() { return sumCnt; };
  38. };
  39.  
  40. int Numerator::sumCnt{ 0 };
  41.  
  42. template <typename T>
  43. void prn(const T& obj)
  44. {
  45.     std::cout << obj << ' ';
  46. }
  47.  
  48. template <typename Action>
  49. void gen(int beg, int end, Action fun)
  50. {
  51.     while (beg++ != end)
  52.     {
  53.         std::cout << fun() << ' ';
  54.     }
  55. }
  56.  
  57. int main()
  58. {
  59.     Numerator n3{ 0,3 };
  60.     Numerator n3_1{ 0,3 };
  61.  
  62.     for (int i{ 0 }; i < 5; ++i)
  63.     {
  64.         std::cout << n3() << ' ';
  65.     }
  66.  
  67.     std::cout << '\n';
  68.    
  69.     gen(0, 5, n3_1);
  70.  
  71.     return 0;
  72.  
  73.     std::vector<int> arr(10);
  74.     std::vector<int> arr1(20);
  75.     std::list<char> symbols{ 'a','s','d','a','s','x','v' };
  76.  
  77.     Printer<int> printer{ ' ' };
  78.     Printer<int> printerNl{ '\n' };
  79.  
  80.     /*std::for_each(arr.begin(), arr.end(),
  81.         [](auto& el) { el = rand() % 10; std::cout << el << ' '; });*/
  82.    
  83.     int cnt{ 1 };
  84.     int delta{ 2 };
  85.  
  86.     Numerator num{1,3};
  87.     Numerator num2x2{ 2,2 };
  88.  
  89.     //std::generate(arr.begin(), arr.end(), [&]() { return cnt+=delta; });
  90.     std::generate(arr.begin(), arr.end(), num);
  91.  
  92.     std::cout << '\n';
  93.     std::for_each(arr.begin(), arr.end(), prn<int>);
  94.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  95.  
  96.     std::cout << '\n';
  97.  
  98.     std::generate(arr1.begin(), arr1.end(), num2x2);
  99.  
  100.     std::cout << '\n';
  101.     std::copy(arr1.begin(), arr1.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  102.  
  103.     std::cout << "Totally Numerated elements: " << Numerator::getSumCnt() << '\n';
  104.  
  105.  
  106.  
  107.  
  108.     /*auto it{ arr.begin() };
  109.  
  110.     while ((it = std::find(it, arr.end(), 1)) != arr.end())
  111.     {
  112.         std::cout << "res is: " << std::distance(arr.begin(), it) << " <=> " << *it << '\n';
  113.         ++it;
  114.     }*/
  115.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  116.     //std::cout << '\n';
  117.  
  118.     //std::stable_partition(arr.begin(), arr.end(), [](int el) { return el % 2; });
  119.  
  120.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  121.     //std::cout << '\n';
  122.  
  123.     //auto res{ std::find(arr.begin(), arr.end(), 0)};
  124.     //auto res{ std::count_if(arr.begin(), arr.end(),[](int el) { return (el % 2 == 0) and (el > 5); }) };
  125.  
  126.  
  127.  
  128.     /*if (res != arr.end())
  129.     {
  130.         std::cout << "res is: " << std::distance(arr.begin(), res) << " <=> " << *res << '\n';
  131.     }
  132.     else
  133.     {
  134.         std::cout << "Not FOUND!!\n";
  135.     }*/
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment