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, void(*action)(T& el))
- {
- while (arrB != arrE)
- {
- if (action) { action(*arrB++); };
- }
- }
- //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++); };
- // }
- //}
- int main()
- {
- const int size{ 10 };
- int arr[size]{ 1,2,3,4,5,6,7,8,9,10 };
- int* arrBegin{ arr };
- int* arrEnd{ arr + size };
- 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