Advertisement
avr39ripe

cppFunctorDraft

Jun 1st, 2021
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. void printElement(T& el)
  5. {
  6.     std::cout << el << ' ';
  7. };
  8.  
  9. template <typename T>
  10. void printElementV(T& el)
  11. {
  12.     std::cout << el << '\n';
  13. };
  14.  
  15. template <typename T>
  16. void randomElement(T& el)
  17. {
  18.     el = rand() % 10;
  19. }
  20.  
  21. template <typename T>
  22. void forEach(T* arrB, T* arrE, void(*action)(T& el))
  23. {
  24.     while (arrB != arrE)
  25.     {
  26.         if (action) { action(*arrB++); };
  27.     }
  28. }
  29.  
  30. int copyEven(int arrS[], int arrSSize, int arrD[], int arrDSize)
  31. {
  32.     int copyCount{ 0 };
  33.     for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
  34.     {
  35.         if (arrS[src] % 2 == 0)
  36.         {
  37.             arrD[dest++] = arrS[src];
  38.             ++copyCount;
  39.         }
  40.  
  41.     }
  42.     return copyCount;
  43. }
  44.  
  45. int copyOdd(int arrS[], int arrSSize, int arrD[], int arrDSize)
  46. {
  47.     int copyCount{ 0 };
  48.     for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
  49.     {
  50.         if (arrS[src] % 2 != 0)
  51.         {
  52.             arrD[dest++] = arrS[src];
  53.             ++copyCount;
  54.         }
  55.  
  56.     }
  57.     return copyCount;
  58. }
  59.  
  60. template <typename T, typename Predicate>
  61. int copy_if(T* srcB, T* srcE, T* destB, T* destE, Predicate pred)
  62. {
  63.     int copyCount{ 0 };
  64.     while (destB != destE and srcB != srcE)
  65.     {
  66.         if (pred(*srcB))
  67.         {
  68.             *destB++ = *srcB;
  69.             ++copyCount;
  70.         }
  71.         ++srcB;
  72.     }
  73.     return copyCount;
  74. }
  75.  
  76. void print(int* begin, int* end, char delimiter = ' ')
  77. {
  78.     while (begin != end)
  79.     {
  80.         std::cout << *begin++ << delimiter;
  81.     }
  82.     std::cout << '\n';
  83. }
  84.  
  85. bool odd(const int el)
  86. {
  87.     return el % 2 != 0;
  88. }
  89.  
  90. bool even(const int el)
  91. {
  92.     return el % 2 == 0;
  93. }
  94.  
  95. bool greater3(const int el)
  96. {
  97.     return el > 3;
  98. }
  99.  
  100. bool all(const int el)
  101. {
  102.     return true;
  103. }
  104.  
  105. class NoSequence
  106. {
  107.     bool init;
  108.     int prevEl;
  109. public:
  110.     NoSequence() : init{false}, prevEl { 0 } {}
  111.  
  112.     bool operator()(int el)
  113.     {
  114.         if (init)
  115.         {
  116.             bool result{ prevEl != el };
  117.             if (result)
  118.             {
  119.                 prevEl = el;
  120.             }
  121.             return result;
  122.         }
  123.         init = true;
  124.         prevEl = el;
  125.         return true;
  126.     }
  127. };
  128.  
  129. class SumLimit
  130. {
  131.     int sumLimit;
  132.     int sum;
  133. public:
  134.     SumLimit(int sumLimitP, int startSumP) : sumLimit{sumLimitP}, sum{startSumP} {}
  135.     SumLimit(int sumLimitP) : SumLimit{ sumLimitP, 0 } {}
  136.  
  137.     bool operator()(int el)
  138.     {
  139.         if (sum + el < sumLimit)
  140.         {
  141.             sum += el;
  142.             return true;
  143.         }
  144.         return false;
  145.     }
  146. };
  147.  
  148. int main()
  149. {
  150.     const int size{ 10 };
  151.     int arr[size]{ 1,1,1,4,5,5,7,8,9,9 };
  152.     int arr1[size]{ 1,2,3,4,5,6,7,8,9,10 };
  153.     int arrE[size]{};
  154.     int* arrBegin{ arr };
  155.     int* arrEnd{ arr + size };
  156.  
  157.  
  158.     print(arr, arr + size);
  159.     print(arrE, arrE + size);
  160.  
  161.     int arrEEnd{ copy_if(arr, arr + size, arrE, arrE + size, NoSequence{}) };
  162.     print(arrE, arrE + arrEEnd);
  163.    
  164.     arrEEnd=  copy_if(arr1, arr1 + size, arrE, arrE + size, SumLimit{16});
  165.     print(arrE, arrE + arrEEnd);
  166.  
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement