Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- void fun()
- {
- std::cout << "fun!\n";
- }
- void printMsg()
- {
- std::cout << "Hello, world!\n";
- }
- int max(int a, int b)
- {
- return a > b ? a : b;
- }
- int min(int a, int b)
- {
- return a < b ? a : b;
- }
- void printArr(int* arr, int arrSize)
- {
- ;
- }
- bool testFun(int a, bool f, char s)
- {
- return false;
- }
- //void printArr(int arr[], int arrSize)
- //{
- // for (int i{ 0 }; i < arrSize; ++i)
- // {
- // std::cout << arr[i] << ' ';
- // }
- // std::cout << '\n';
- //}
- //
- //void randomizeArr(int arr[], int arrSize, int minVal, int maxVal)
- //{
- // for (int i{ 0 }; i < arrSize; ++i)
- // {
- // arr[i] = rand() % (maxVal - minVal) + minVal;
- // }
- //}
- //template <typename T>
- //void forEach(T* arrB, T* arrE, int action = 0)
- //{
- // while (arrB != arrE)
- // {
- // if (action == 0)
- // {
- // printElement(*arrB);
- // }
- // else if ( action == 1)
- // {
- // randomElement(*arrB);
- // }
- // ++arrB;
- // }
- //}
- 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;
- }
- //void forEach(int* arrB, int* arrE, void(*action)(int& el))
- //{
- // while (arrB != arrE)
- // {
- // if (action) { action(*arrB++); };
- // }
- //}
- 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>
- int copy_if(T* srcB, T* srcE, T* destB, T* destE, bool(*pred)(const T& elem))
- {
- int copyCount{ 0 };
- while (destB != destE and srcB != srcE)
- {
- if (pred(*srcB))
- {
- *destB++ = *srcB;
- ++copyCount;
- }
- ++srcB;
- }
- return copyCount;
- }
- //void print(int arr[], int arrSize)
- //{
- // for (int i{ 0 }; i < arrSize; ++i)
- // {
- // std::cout << arr[i] << ' ';
- // }
- // std::cout << '\n';
- //}
- void print(int* begin, int* end)
- {
- while (begin != end )
- {
- std::cout << *begin << ' ';
- ++begin;
- }
- 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;
- }
- int main()
- {
- const int size{ 10 };
- int arr[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{ copyOdd(arr, size, arrE, size) };
- int arrEEnd{ copy_if(arr+2, arr + 8, arrE, arrE + size, even) };
- print(arrE, arrE + arrEEnd);
- /*forEach(arrBegin, arrEnd, printElement);
- forEach(arrBegin+2, arrEnd-2, randomElement);
- std::cout << '\n';
- forEach(arrBegin, arrEnd, printElementV);
- std::cout << '\n';*/
- return 0;
- int x{ 42 };
- float temp{ 42.2 };
- bool flag{ true };
- char symb{ 'a' };
- int* ptrI{ &x };
- void* vPtr{ &fun };
- //int* fptr{ fun };
- void(*funcPtr)() {printMsg};
- void(*prnPtr)(int*, int) { printArr };
- std::function<void(int*, int)> prn { printArr };
- int num{ 44 };
- //void printArr(int* arr, int arrSize)
- //bool testFun(int a, bool f, char s)
- bool(*testPtr)(int, bool, char) { testFun };
- std::function<bool(int, bool, char)> nTestPtr{ testFun };
- void(*fptr)() { fun };
- //void(mPtr)() { max };
- int(*mPtr)(int, int) { nullptr };
- int menu;
- std::cout << "Max = 1\nMin = 2\n";
- std::cin >> menu;
- if (menu == 1) mPtr = max;
- if (menu == 2) mPtr = min;
- if (mPtr)
- {
- std::cout << mPtr(10, 20) << '\n';
- }
- else
- {
- std::cout << "mPtr == nullptr!!! Error!\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement