Advertisement
avr39ripe

PV913FunctorsExample

Aug 19th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 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. int main()
  43. {
  44.     std::vector<int> arr(10);
  45.     std::vector<int> arr1(20);
  46.     std::list<char> symbols{ 'a','s','d','a','s','x','v' };
  47.  
  48.     Printer<int> printer{ ' ' };
  49.     Printer<int> printerNl{ '\n' };
  50.  
  51.     /*std::for_each(arr.begin(), arr.end(),
  52.         [](auto& el) { el = rand() % 10; std::cout << el << ' '; });*/
  53.    
  54.     int cnt{ 1 };
  55.     int delta{ 2 };
  56.  
  57.     Numerator num{1,3};
  58.     Numerator num2x2{ 2,2 };
  59.  
  60.     //std::generate(arr.begin(), arr.end(), [&]() { return cnt+=delta; });
  61.     std::generate(arr.begin(), arr.end(), num);
  62.  
  63.     std::cout << '\n';
  64.     std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  65.  
  66.     std::cout << '\n';
  67.  
  68.     std::generate(arr1.begin(), arr1.end(), num2x2);
  69.  
  70.     std::cout << '\n';
  71.     std::copy(arr1.begin(), arr1.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  72.  
  73.     std::cout << "Totally Numerated elements: " << Numerator::getSumCnt() << '\n';
  74.  
  75.     /*auto it{ arr.begin() };
  76.  
  77.     while ((it = std::find(it, arr.end(), 1)) != arr.end())
  78.     {
  79.         std::cout << "res is: " << std::distance(arr.begin(), it) << " <=> " << *it << '\n';
  80.         ++it;
  81.     }*/
  82.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  83.     //std::cout << '\n';
  84.  
  85.     //std::stable_partition(arr.begin(), arr.end(), [](int el) { return el % 2; });
  86.  
  87.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  88.     //std::cout << '\n';
  89.  
  90.     //auto res{ std::find(arr.begin(), arr.end(), 0)};
  91.     //auto res{ std::count_if(arr.begin(), arr.end(),[](int el) { return (el % 2 == 0) and (el > 5); }) };
  92.  
  93.  
  94.  
  95.     /*if (res != arr.end())
  96.     {
  97.         std::cout << "res is: " << std::distance(arr.begin(), res) << " <=> " << *res << '\n';
  98.     }
  99.     else
  100.     {
  101.         std::cout << "Not FOUND!!\n";
  102.     }*/
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement