Advertisement
avr39-ripe

stlDiffelementsOfTwoarray

Feb 11th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <vector>
  5. #include <set>
  6.  
  7.  
  8. template <typename InputIter, typename GenFunc>
  9. void randomize(InputIter begin, InputIter end, GenFunc generator)
  10. {
  11.     while(begin != end)
  12.     {
  13.         *begin++ = generator();
  14.     }
  15. }
  16.  
  17. template <typename InputIter>
  18. void print(InputIter begin, InputIter end)
  19. {
  20.     while(begin != end)
  21.     {
  22.         std::cout << *begin++ << ' ';
  23.     }
  24.     std::cout << '\n';
  25. }
  26.  
  27.  
  28. template <typename InputIter, typename OutputIter>
  29. void dedup(InputIter begin, InputIter end, OutputIter out)
  30. {
  31.     InputIter next{begin};
  32.     ++next;
  33.  
  34.     while(begin != end)
  35.     {
  36.         if( *begin != *next)
  37.         {
  38.             *out++ = *begin++;
  39.             ++next;
  40.         }
  41.         else
  42.         {
  43.             advance(begin,2);
  44.             advance(next,2);
  45.         }
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     std::vector<int> arr(20);
  52.     std::vector<int> arr1(25);
  53.  
  54.     std::vector<int> arrSet(20);
  55.     std::vector<int> arr1Set(25);
  56.  
  57.  
  58.     randomize(arr.begin(),arr.end(),[](){return rand()%30+10;});
  59.     randomize(arr1.begin(),arr1.end(),[](){return rand()%30+10;});
  60.  
  61.     std::copy(arr.begin(),arr.end(),arrSet.begin());
  62.     std::copy(arr1.begin(),arr1.end(),arr1Set.begin());
  63.  
  64.     print(arr.begin(),arr.end());
  65.     print(arr1.begin(),arr1.end());
  66.     std::cout << '\n';
  67.  
  68.     std::sort(arr.begin(),arr.end());
  69.     std::sort(arr1.begin(),arr1.end());
  70.     std::cout << "\nSorted containers\n";
  71.     print(arr.begin(),arr.end());
  72.     print(arr1.begin(),arr1.end());
  73.     std::cout << '\n';
  74.  
  75.     auto uniqeIt{std::unique(arr.begin(),arr.end())};
  76.     auto uniqeIt1{std::unique(arr1.begin(),arr1.end())};
  77.     std::cout << "\nUniqued, but not erased containers\n";
  78.     print(arr.begin(),arr.end());
  79.     print(arr1.begin(),arr1.end());
  80.     std::cout << '\n';
  81.  
  82.     arr.erase(uniqeIt,arr.end());
  83.     arr1.erase(uniqeIt1,arr1.end());
  84.     std::cout << "\nUniqued containers\n";
  85.     print(arr.begin(),arr.end());
  86.     print(arr1.begin(),arr1.end());
  87.     std::cout << '\n';
  88.  
  89.     auto isGood = [&arr1](int el){return !(std::binary_search(arr1.begin(), arr1.end(), el));};
  90.     auto isGood1 = [&arr](int el){return !(std::binary_search(arr.begin(), arr.end(), el));};
  91.  
  92.     std::vector<int> res;
  93.  
  94.     std::copy_if(arr.begin(),arr.end(),std::inserter(res,res.end()),isGood);
  95.     std::copy_if(arr1.begin(),arr1.end(),std::inserter(res,res.end()),isGood1);
  96.     std::sort(res.begin(),res.end());
  97.     std::cout << "\nResult with sort+unique+binary_search\n";
  98.     print(res.begin(),res.end());
  99.  
  100.     std::cout << "\nAlternative method\n";
  101.     std::vector<int> join(arr.size() + arr1.size());
  102.     std::merge(arr.begin(),arr.end(),arr1.begin(),arr1.end(),join.begin());
  103.     print(join.begin(),join.end());
  104.  
  105.     std::cout << "\nAlternative result\n";
  106.  
  107.     std::vector<int> resAlt;
  108.     dedup(join.begin(), join.end(), std::inserter(resAlt,resAlt.end()));
  109.     print(resAlt.begin(),resAlt.end());
  110.  
  111.     std::cout << "\nSame task using std::set\n";
  112.  
  113.     std::set<int> resSet;
  114.     std::set<int> res1Set;
  115.  
  116.     for(auto& el: arrSet)
  117.     {
  118.         resSet.insert(el);
  119.     }
  120.     for(auto& el: arr1Set)
  121.     {
  122.         res1Set.insert(el);
  123.     }
  124.  
  125.     std::vector<int> result;
  126.     std::set_difference(resSet.begin(),resSet.end(), res1Set.begin(),res1Set.end(), std::inserter(result,result.end()));
  127.     std::set_difference(res1Set.begin(),res1Set.end(), resSet.begin(),resSet.end(), std::inserter(result,result.end()));
  128.     std::sort(result.begin(),result.end());
  129.     print(result.begin(), result.end());
  130.     std::cout << '\n';
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement