Advertisement
avr39ripe

PV024FPtrBase

Dec 10th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 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. template <typename T>
  54. void forEach(T* arrB, T* arrE, void(*action)(T& el))
  55. {
  56.     while (arrB != arrE)
  57.     {
  58.         if (action) { action(*arrB++); };
  59.     }
  60. }
  61.  
  62. //template <typename T>
  63. //void forEach(T* arrB, T* arrE, int action = 0)
  64. //{
  65. //  while (arrB != arrE)
  66. //  {
  67. //      if (action == 0)
  68. //      {
  69. //          printElement(*arrB);
  70. //      }
  71. //      else if ( action == 1)
  72. //      {
  73. //          randomElement(*arrB);
  74. //      }
  75. //      ++arrB;
  76. //  }
  77. //}
  78.  
  79. template <typename T>
  80. void printElement(T& el)
  81. {
  82.     std::cout << el << ' ';
  83. };
  84.  
  85. template <typename T>
  86. void printElementV(T& el)
  87. {
  88.     std::cout << el << '\n';
  89. };
  90.  
  91. template <typename T>
  92. void randomElement(T& el)
  93. {
  94.     el = rand() % 10;
  95. }
  96.  
  97. //void forEach(int* arrB, int* arrE, void(*action)(int& el))
  98. //{
  99. //  while (arrB != arrE)
  100. //  {
  101. //      if (action) { action(*arrB++); };
  102. //  }
  103. //}
  104.  
  105. int main()
  106. {
  107.     const int size{ 10 };
  108.     int arr[size]{ 1,2,3,4,5,6,7,8,9,10 };
  109.     int* arrBegin{ arr };
  110.     int* arrEnd{ arr + size };
  111.  
  112.     forEach(arrBegin, arrEnd, printElement);
  113.     forEach(arrBegin+2, arrEnd-2, randomElement);
  114.  
  115.     std::cout << '\n';
  116.     forEach(arrBegin, arrEnd, printElementV);
  117.  
  118.     std::cout << '\n';
  119.  
  120.     return 0;
  121.  
  122.     int x{ 42 };
  123.     float temp{ 42.2 };
  124.     bool flag{ true };
  125.     char symb{ 'a' };
  126.     int* ptrI{ &x };
  127.  
  128.     void* vPtr{ &fun };
  129.    
  130.     //int* fptr{ fun };
  131.  
  132.     void(*funcPtr)() {printMsg};
  133.    
  134.  
  135.  
  136.     void(*prnPtr)(int*, int) { printArr };
  137.     std::function<void(int*, int)> prn { printArr };
  138.  
  139.     int num{ 44 };
  140.     //void printArr(int* arr, int arrSize)
  141.    
  142.  
  143.     //bool testFun(int a, bool f, char s)
  144.  
  145.     bool(*testPtr)(int, bool, char) { testFun };
  146.     std::function<bool(int, bool, char)> nTestPtr{ testFun };
  147.  
  148.     void(*fptr)() { fun };
  149.     //void(mPtr)() { max };
  150.  
  151.     int(*mPtr)(int, int) { nullptr };
  152.     int menu;
  153.     std::cout << "Max = 1\nMin = 2\n";
  154.  
  155.     std::cin >> menu;
  156.  
  157.     if (menu == 1) mPtr = max;
  158.     if (menu == 2) mPtr = min;
  159.  
  160.     if (mPtr)
  161.     {
  162.         std::cout << mPtr(10, 20) << '\n';
  163.     }
  164.     else
  165.     {
  166.         std::cout << "mPtr == nullptr!!! Error!\n";
  167.     }
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement