Advertisement
avr39ripe

PV024FPtrAdv

Dec 16th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. void fun()
  5. {
  6.     std::cout << "fun!\n";
  7. }
  8.  
  9. void printMsg()
  10. {
  11.     std::cout << "Hello, world!\n";
  12. }
  13.  
  14. int max(int a, int b)
  15. {
  16.     return a > b ? a : b;
  17. }
  18.  
  19. int min(int a, int b)
  20. {
  21.     return a < b ? a : b;
  22. }
  23.  
  24. void printArr(int* arr, int arrSize)
  25. {
  26.     ;
  27. }
  28.  
  29. bool testFun(int a, bool f, char s)
  30. {
  31.     return false;
  32. }
  33.  
  34.  
  35. //void printArr(int arr[], int arrSize)
  36. //{
  37. //  for (int i{ 0 }; i < arrSize; ++i)
  38. //  {
  39. //      std::cout << arr[i] << ' ';
  40. //  }
  41. //  std::cout << '\n';
  42. //}
  43. //
  44. //void randomizeArr(int arr[], int arrSize, int minVal, int maxVal)
  45. //{
  46. //  for (int i{ 0 }; i < arrSize; ++i)
  47. //  {
  48. //      arr[i] = rand() % (maxVal - minVal) + minVal;
  49. //  }
  50. //}
  51.  
  52.  
  53.  
  54.  
  55. //template <typename T>
  56. //void forEach(T* arrB, T* arrE, int action = 0)
  57. //{
  58. //  while (arrB != arrE)
  59. //  {
  60. //      if (action == 0)
  61. //      {
  62. //          printElement(*arrB);
  63. //      }
  64. //      else if ( action == 1)
  65. //      {
  66. //          randomElement(*arrB);
  67. //      }
  68. //      ++arrB;
  69. //  }
  70. //}
  71.  
  72. template <typename T>
  73. void printElement(T& el)
  74. {
  75.     std::cout << el << ' ';
  76. };
  77.  
  78. template <typename T>
  79. void printElementV(T& el)
  80. {
  81.     std::cout << el << '\n';
  82. };
  83.  
  84. template <typename T>
  85. void randomElement(T& el)
  86. {
  87.     el = rand() % 10;
  88. }
  89.  
  90. //void forEach(int* arrB, int* arrE, void(*action)(int& el))
  91. //{
  92. //  while (arrB != arrE)
  93. //  {
  94. //      if (action) { action(*arrB++); };
  95. //  }
  96. //}
  97.  
  98. template <typename T>
  99. void forEach(T* arrB, T* arrE, void(*action)(T& el))
  100. {
  101.     while (arrB != arrE)
  102.     {
  103.         if (action) { action(*arrB++); };
  104.     }
  105. }
  106.  
  107. int copyEven(int arrS[], int arrSSize, int arrD[], int arrDSize)
  108. {
  109.     int copyCount{ 0 };
  110.     for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
  111.     {
  112.         if (arrS[src] % 2 == 0)
  113.         {
  114.             arrD[dest++] = arrS[src];
  115.             ++copyCount;
  116.         }
  117.  
  118.     }
  119.     return copyCount;
  120. }
  121.  
  122. int copyOdd(int arrS[], int arrSSize, int arrD[], int arrDSize)
  123. {
  124.     int copyCount{ 0 };
  125.     for (int src{ 0 }, dest{ 0 }; src < arrSSize; ++src)
  126.     {
  127.         if (arrS[src] % 2 != 0)
  128.         {
  129.             arrD[dest++] = arrS[src];
  130.             ++copyCount;
  131.         }
  132.  
  133.     }
  134.     return copyCount;
  135. }
  136.  
  137. template <typename T>
  138. int copy_if(T* srcB, T* srcE, T* destB, T* destE, bool(*pred)(const T& elem))
  139. {
  140.     int copyCount{ 0 };
  141.     while (destB != destE and srcB != srcE)
  142.     {
  143.         if (pred(*srcB))
  144.         {
  145.             *destB++ = *srcB;
  146.             ++copyCount;
  147.         }
  148.         ++srcB;
  149.     }
  150.     return copyCount;
  151. }
  152.  
  153. //void print(int arr[], int arrSize)
  154. //{
  155. //  for (int i{ 0 }; i < arrSize; ++i)
  156. //  {
  157. //      std::cout << arr[i] << ' ';
  158. //  }
  159. //  std::cout << '\n';
  160. //}
  161.  
  162. void print(int* begin, int* end)
  163. {
  164.     while (begin != end )
  165.     {
  166.         std::cout << *begin << ' ';
  167.         ++begin;
  168.     }
  169.     std::cout << '\n';
  170. }
  171.  
  172. bool odd(const int& el)
  173. {
  174.     return el % 2 != 0;
  175. }
  176.  
  177. bool even(const int& el)
  178. {
  179.     return el % 2 == 0;
  180. }
  181.  
  182. bool greater3(const int& el)
  183. {
  184.     return el > 3;
  185. }
  186.  
  187. bool all(const int& el)
  188. {
  189.     return true;
  190. }
  191.  
  192. int main()
  193. {
  194.     const int size{ 10 };
  195.     int arr[size]{ 1,2,3,4,5,6,7,8,9,10 };
  196.     int arrE[size]{};
  197.     int* arrBegin{ arr };
  198.     int* arrEnd{ arr + size };
  199.  
  200.  
  201.     print(arr, arr+size);
  202.     print(arrE, arrE + size);
  203.     //int arrEEnd{ copyOdd(arr, size, arrE, size) };
  204.     int arrEEnd{ copy_if(arr+2, arr + 8, arrE, arrE + size, even) };
  205.     print(arrE, arrE + arrEEnd);
  206.  
  207.     /*forEach(arrBegin, arrEnd, printElement);
  208.     forEach(arrBegin+2, arrEnd-2, randomElement);
  209.  
  210.     std::cout << '\n';
  211.     forEach(arrBegin, arrEnd, printElementV);
  212.  
  213.     std::cout << '\n';*/
  214.  
  215.     return 0;
  216.  
  217.     int x{ 42 };
  218.     float temp{ 42.2 };
  219.     bool flag{ true };
  220.     char symb{ 'a' };
  221.     int* ptrI{ &x };
  222.  
  223.     void* vPtr{ &fun };
  224.    
  225.     //int* fptr{ fun };
  226.  
  227.     void(*funcPtr)() {printMsg};
  228.    
  229.  
  230.  
  231.     void(*prnPtr)(int*, int) { printArr };
  232.     std::function<void(int*, int)> prn { printArr };
  233.  
  234.     int num{ 44 };
  235.     //void printArr(int* arr, int arrSize)
  236.    
  237.  
  238.     //bool testFun(int a, bool f, char s)
  239.  
  240.     bool(*testPtr)(int, bool, char) { testFun };
  241.     std::function<bool(int, bool, char)> nTestPtr{ testFun };
  242.  
  243.     void(*fptr)() { fun };
  244.     //void(mPtr)() { max };
  245.  
  246.     int(*mPtr)(int, int) { nullptr };
  247.     int menu;
  248.     std::cout << "Max = 1\nMin = 2\n";
  249.  
  250.     std::cin >> menu;
  251.  
  252.     if (menu == 1) mPtr = max;
  253.     if (menu == 2) mPtr = min;
  254.  
  255.     if (mPtr)
  256.     {
  257.         std::cout << mPtr(10, 20) << '\n';
  258.     }
  259.     else
  260.     {
  261.         std::cout << "mPtr == nullptr!!! Error!\n";
  262.     }
  263.  
  264.     return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement