Advertisement
avr39ripe

cppSortFPtrAdvanced

Sep 22nd, 2021
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename T> void sort(T* begin, T* const end, bool(*sortCrit)(const T&, const T&), void(*print)(const T*, const T*) = nullptr)
  4. {
  5.     if (print) print(begin, end);
  6.     T tmp;
  7.     for (T* head{ begin }; head != end; ++head)
  8.     {
  9.         for (T* tail{ end - 1 }; tail != head; --tail)
  10.         {
  11.             if (sortCrit(*tail, *head))
  12.             {
  13.                 tmp = *tail;
  14.                 *tail = *head;
  15.                 *head = tmp;
  16.                 if (print) print(begin, end);
  17.             }
  18.         }
  19.  
  20.     }
  21.     if (print) print(begin, end);
  22. }
  23.  
  24. template <typename T>
  25. void print(const T* begin, const T* const end)
  26. {
  27.     while (begin != end)
  28.     {
  29.         std::cout << *begin++ << ' ';
  30.     }
  31.     std::cout << '\n';
  32. }
  33.  
  34. template <typename T> bool ascending(const T& a, const T& b)
  35. {
  36.     return a < b;
  37. }
  38.  
  39. template <typename T> bool descending(const T& a, const T& b)
  40. {
  41.     return a > b;
  42. }
  43.  
  44. template <typename T> bool even(const T& a, const T& b)
  45. {
  46.     return a % 2 == 0 and b % 2 !=0;
  47. }
  48.  
  49. template <typename T> bool odd(const T& a, const T& b)
  50. {
  51.     return a % 2 != 0 and b % 2 == 0;
  52. }
  53.  
  54. template <typename T, bool(*sortTrue)(const T&, const T&), bool(*sortFalse)(const T&, const T&), bool(*criteria)(const T&, const T&)>
  55. bool parityOrdered(const T& a, const T& b)
  56. {
  57.     return sortTrue(a,b) or (!sortFalse(a,b) and criteria(a, b));
  58. }
  59.  
  60. int main()
  61. {
  62.     const int arrASize{ 15 };
  63.     const int arrBSize{ 15 };
  64.     int arrA[arrASize]{ 6,1,4,2,8,9,4,26,2,7,8,11,7,2,3 };
  65.     int arrB[arrBSize]{ 1,8,29,2,2,9,11,1,2,5,8,34,12,9,8 };
  66.  
  67.     std::cout << "Original arrays:\n";
  68.     print(arrA, arrA + arrASize);
  69.     print(arrB, arrB + arrBSize);
  70.  
  71.     std::cout << "Sorted arrays ascending/descending:\n";
  72.     sort(arrA, arrA + arrASize, ascending<int>);
  73.     sort(arrB, arrB + arrBSize, descending<int>);
  74.     print(arrA, arrA + arrASize);
  75.     print(arrB, arrB + arrBSize);
  76.     std::cout << "Sorted arrays even/odd:\n";
  77.     sort(arrA, arrA + arrASize, even<int>);
  78.     sort(arrB, arrB + arrBSize, odd<int>);
  79.     print(arrA, arrA + arrASize);
  80.     print(arrB, arrB + arrBSize);
  81.  
  82.     std::cout << "Sorted arrays parity ordered: even-odd descending / odd-even ascending:\n";
  83.     sort(arrA, arrA + arrASize, parityOrdered<int, even, odd, descending>);
  84.     sort(arrB, arrB + arrBSize, parityOrdered<int, odd, even, ascending>);
  85.     print(arrA, arrA + arrASize);
  86.     print(arrB, arrB + arrBSize);
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement