Advertisement
avr39ripe

PV913ArreyMegaExampleSTL

Aug 18th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. template <class InIter>
  6. InIter dup_only(InIter begin, InIter end)
  7. {
  8.     InIter out{ begin };
  9.     while (begin != end and std::next(begin) != end)
  10.     {
  11.         if (*begin == *std::next(begin))
  12.             *out++ = std::move(*begin++);
  13.         if (begin != end)
  14.             ++begin;
  15.     }
  16.     return out;
  17. }
  18.  
  19. template <class InIter>
  20. InIter dedup(InIter begin, InIter end)
  21. {
  22.     InIter out{ begin };
  23.     while (begin != end and std::next(begin) != end)
  24.     {
  25.         if (*begin != *std::next(begin))
  26.             *out++ = std::move(*begin++);
  27.         else
  28.             if (++begin != end)
  29.                 ++begin;
  30.     }
  31.     if (begin != end)
  32.         *out++ = std::move(*begin);
  33.     return out;
  34. }
  35.  
  36. int main()
  37. {
  38.     char input{ 'x' };
  39.     std::vector<int> arrA{ 6,1,4,2,8,9,11,26,2,7,8,11,2,2,3 };
  40.     std::vector<int> arrB{ 1,8,29,2,2,9,11,1,2,5,8,34,12,9,8 };
  41.  
  42.     std::sort(arrA.begin(), arrA.end());
  43.     std::sort(arrB.begin(), arrB.end());
  44.  
  45.     std::copy(arrA.begin(), arrA.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  46.     std::copy(arrB.begin(), arrB.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  47.     std::cout << '\n';
  48.  
  49.     auto aUnique{ std::unique(arrA.begin(), arrA.end()) };
  50.     auto bUnique{ std::unique(arrB.begin(), arrB.end()) };
  51.  
  52.     std::copy(arrA.begin(), aUnique, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  53.     std::copy(arrB.begin(), bUnique, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  54.     std::cout << '\n';
  55.  
  56.     do
  57.     {
  58.         std::cout << "\na - Only different elements from A and B without duplicates\n";
  59.         std::cout << "b - Only common elements from A and B without duplicates\n";
  60.         std::cout << "c - Only elements from A that not in B without duplicates\n";
  61.         std::cout << "d - Only elements from B that not in A without duplicates\n";
  62.         std::cin >> input; input = tolower(input);
  63.         std::cout << '\n';
  64.         if (input == 'a')
  65.         {
  66.             std::vector<int> res(std::distance(arrA.begin(), aUnique) + std::distance(arrB.begin(), bUnique));
  67.             std::merge(arrA.begin(), aUnique, arrB.begin(), bUnique, res.begin());
  68.             std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  69.             std::cout << '\n';
  70.             auto resEnd{ dedup(res.begin(), res.end()) };
  71.             std::copy(res.begin(), resEnd, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  72.         }
  73.         else if (input == 'b')
  74.         {
  75.             std::vector<int> res(std::distance(arrA.begin(), aUnique) + std::distance(arrB.begin(), bUnique));
  76.             std::merge(arrA.begin(), aUnique, arrB.begin(), bUnique, res.begin());
  77.             std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  78.             std::cout << '\n';
  79.             auto resEnd{ dup_only(res.begin(), res.end()) };
  80.             std::copy(res.begin(), resEnd, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
  81.         }
  82.         else if (input == 'c')
  83.         {
  84.             auto aEnd{ std::copy_if(arrA.begin(), aUnique, arrA.begin(),
  85.             [&arrB](const auto& el) { return !(std::binary_search(arrB.begin(), arrB.end(), el)); }) };
  86.  
  87.             std::copy(arrA.begin(), aEnd, std::ostream_iterator<int>(std::cout, " "));
  88.             std::cout << '\n';
  89.         }
  90.         else if (input == 'd')
  91.         {
  92.             auto bEnd{ std::copy_if(arrB.begin(), bUnique, arrB.begin(),
  93.             [&arrA](const auto& el) { return !(std::binary_search(arrA.begin(), arrA.end(), el)); }) };
  94.  
  95.             std::copy(arrB.begin(), bEnd, std::ostream_iterator<int>(std::cout, " "));
  96.             std::cout << '\n';
  97.         }
  98.     } while (input != 'x');
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement