avr39ripe

cppArrayDiffElementOfTwoArraySTLLikeNew

Oct 4th, 2021
605
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. template <typename T> bool sortUp(T a, T b)
  4. {
  5.     return a < b;
  6. }
  7.  
  8. template <typename T> bool sortDown(T a, T b)
  9. {
  10.     return a > b;
  11. }
  12.  
  13. template <typename T> void sort(T* begin, T* end, bool(*sortCrit)(T, T), void(*print)(T*, T*) = nullptr)
  14. {
  15.     if (print) print(begin, end);
  16.     T tmp;
  17.     for (T* head{ begin }; head != end; ++head)
  18.     {
  19.         for (T* tail{ end - 1 }; tail != head; --tail)
  20.         {
  21.             if (sortCrit(*tail, *head))
  22.             {
  23.                 tmp = *tail;
  24.                 *tail = *head;
  25.                 *head = tmp;
  26.                 if (print) print(begin, end);
  27.             }
  28.         }
  29.  
  30.     }
  31.     if (print) print(begin, end);
  32. }
  33.  
  34. template <typename T>
  35. void print(T* begin, T* end)
  36. {
  37.     while (begin != end)
  38.     {
  39.         std::cout << *begin++ << ' ';
  40.     }
  41.     std::cout << '\n';
  42. }
  43.  
  44. template <typename T>
  45. T* unique(T* begin, T* end)
  46. {
  47.     T* insertPos{ begin + 1 };
  48.     for (; (insertPos != end) and (*insertPos != *(insertPos - 1)); ++insertPos);
  49.     if (insertPos == end) return end;
  50.  
  51.     begin = insertPos + 1;
  52.     while (begin != end)
  53.     {
  54.         if (*(insertPos - 1) != *begin)
  55.         {
  56.             *insertPos++ = *begin;
  57.         }
  58.         ++begin;
  59.     }
  60.     return insertPos;
  61. }
  62.  
  63. template <typename T>
  64. T* uniqualize(T* begin, T* end)
  65. {
  66.     sort(begin, end, sortUp<int>);
  67.     return unique(begin, end);
  68. }
  69.  
  70. template <typename T>
  71. T* merge(T* beginA, T* endA, T* beginB, T* endB, T* beginDest)
  72. {
  73.     while (beginA != endA and beginB != endB)
  74.     {
  75.         *beginDest++ = *beginA < *beginB ? *beginA++ : *beginB++;
  76.     }
  77.     while (beginA != endA)
  78.     {
  79.         *beginDest++ = *beginA++;
  80.     }
  81.     while (beginB != endB)
  82.     {
  83.         *beginDest++ = *beginB++;
  84.     }
  85.     return beginDest;
  86. }
  87.  
  88. template <typename T>
  89. T* dedup(T* begin, T* end)
  90. {
  91.     T* out{ begin };
  92.     while (begin != end and (begin + 1) != end)
  93.     {
  94.         if (*begin != *(begin + 1))
  95.             *out++ = *begin++;
  96.         else
  97.             if (++begin != end)
  98.                 ++begin;
  99.     }
  100.     if (begin != end)
  101.         *out++ = *begin;
  102.     return out;
  103. }
  104.  
  105. template <typename T>
  106. size_t distance(T* begin, T* end)
  107. {
  108.     return end - begin;
  109. }
  110.  
  111. template <typename T>
  112. size_t copy(T* srcB, T* srcE, T* destB, T* destE)
  113. {
  114.     size_t copyCount{ 0 };
  115.     while (destB != destE and srcB != srcE)
  116.     {
  117.         *destB++ = *srcB++;
  118.         ++copyCount;
  119.     }
  120.     return copyCount;
  121. }
  122.  
  123. int* createArr(size_t size)
  124. {
  125.     return new int[size];
  126. }
  127.  
  128. void deleteArr(int* ptr)
  129. {
  130.     delete[] ptr;
  131. }
  132.  
  133. int main()
  134. {
  135.     const int arrASize{ 15 };
  136.     const int arrBSize{ 15 };
  137.     int arrA[arrASize] = { 6,1,4,2,8,9,11,26,2,7,8,11,2,2,3 };
  138.     int arrB[arrBSize] = { 1,8,29,2,2,9,11,1,2,5,8,34,12,9,8 };
  139.  
  140.     std::cout << "Original arrays:\n";
  141.     print(arrA, arrA + arrASize);
  142.     print(arrB, arrB + arrBSize);
  143.  
  144.     int* uniqueAPos{ uniqualize(arrA, arrA + arrASize) };
  145.     int* uniqueBPos{ uniqualize(arrB, arrB + arrBSize) };
  146.  
  147.     int* mergeAB{ createArr(distance(arrA,uniqueAPos) + distance(arrB,uniqueBPos)) };
  148.     int* mergePos{ merge(arrA, uniqueAPos,arrB, uniqueBPos, mergeAB) };
  149.     //std::cout << "\nMerged elements of two arrays:\n";
  150.     //print(mergeAB, mergePos);
  151.     int* dedupPos{ dedup(mergeAB, mergePos) };
  152.     //std::cout << "\nDifferent elements of two arrays without duplicates:\n";
  153.     //print(mergeAB, dedupPos);
  154.  
  155.     auto arrCSize{ distance(mergeAB, dedupPos) };
  156.     auto arrC{ createArr(arrCSize) };
  157.     copy(mergeAB, dedupPos, arrC, arrC + arrCSize);
  158.     std::cout << "\nDifferent elements of two arrays without duplicates:\n";
  159.     print(arrC, arrC + arrCSize);
  160.  
  161.     deleteArr(mergeAB);
  162.     deleteArr(arrC);
  163. }
RAW Paste Data