Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename T>
- void printElement(T& el)
- {
- std::cout << el << ' ';
- };
- template <typename T>
- void printElementV(T& el)
- {
- std::cout << el << '\n';
- };
- template <typename T>
- void randomElement(T& el)
- {
- el = rand() % 10;
- }
- template <typename T>
- void forEach(T* arrB, T* arrE, void(*action)(T& el))
- {
- while (arrB != arrE)
- {
- if (action) { action(*arrB++); };
- }
- }
- int copyEven(int arrS[], int arrSSize, int arrD[], int arrDSize)
- {
- int copyCount{ 0 };
- for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
- {
- if (arrS[src] % 2 == 0)
- {
- arrD[dest++] = arrS[src];
- ++copyCount;
- }
- }
- return copyCount;
- }
- int copyOdd(int arrS[], int arrSSize, int arrD[], int arrDSize)
- {
- int copyCount{ 0 };
- for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
- {
- if (arrS[src] % 2 != 0)
- {
- arrD[dest++] = arrS[src];
- ++copyCount;
- }
- }
- return copyCount;
- }
- template <typename T, typename Predicate>
- int copy_if(T* srcB, T* srcE, T* destB, T* destE, Predicate pred)
- {
- int copyCount{ 0 };
- while (destB != destE and srcB != srcE)
- {
- if (pred(*srcB))
- {
- *destB++ = *srcB;
- ++copyCount;
- }
- ++srcB;
- }
- return copyCount;
- }
- void print(int* begin, int* end, char delimiter = ' ')
- {
- while (begin != end)
- {
- std::cout << *begin++ << delimiter;
- }
- std::cout << '\n';
- }
- bool odd(const int el)
- {
- return el % 2 != 0;
- }
- bool even(const int el)
- {
- return el % 2 == 0;
- }
- bool greater3(const int el)
- {
- return el > 3;
- }
- bool all(const int el)
- {
- return true;
- }
- class NoSequence
- {
- bool init;
- int prevEl;
- public:
- NoSequence() : init{false}, prevEl { 0 } {}
- bool operator()(int el)
- {
- if (init)
- {
- bool result{ prevEl != el };
- if (result)
- {
- prevEl = el;
- }
- return result;
- }
- init = true;
- prevEl = el;
- return true;
- }
- };
- class SumLimit
- {
- int sumLimit;
- int sum;
- public:
- SumLimit(int sumLimitP, int startSumP) : sumLimit{sumLimitP}, sum{startSumP} {}
- SumLimit(int sumLimitP) : SumLimit{ sumLimitP, 0 } {}
- bool operator()(int el)
- {
- if (sum + el < sumLimit)
- {
- sum += el;
- return true;
- }
- return false;
- }
- };
- int main()
- {
- const int size{ 10 };
- int arr[size]{ 1,1,1,4,5,5,7,8,9,9 };
- int arr1[size]{ 1,2,3,4,5,6,7,8,9,10 };
- int arrE[size]{};
- int* arrBegin{ arr };
- int* arrEnd{ arr + size };
- print(arr, arr + size);
- print(arrE, arrE + size);
- int arrEEnd{ copy_if(arr, arr + size, arrE, arrE + size, NoSequence{}) };
- print(arrE, arrE + arrEEnd);
- arrEEnd= copy_if(arr1, arr1 + size, arrE, arrE + size, SumLimit{16});
- print(arrE, arrE + arrEEnd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement