Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <iterator>
- #include <vector>
- #include <set>
- template <typename InputIter, typename GenFunc>
- void randomize(InputIter begin, InputIter end, GenFunc generator)
- {
- while(begin != end)
- {
- *begin++ = generator();
- }
- }
- template <typename InputIter>
- void print(InputIter begin, InputIter end)
- {
- while(begin != end)
- {
- std::cout << *begin++ << ' ';
- }
- std::cout << '\n';
- }
- template <typename InputIter, typename OutputIter>
- void dedup(InputIter begin, InputIter end, OutputIter out)
- {
- InputIter next{begin};
- ++next;
- while(begin != end)
- {
- if( *begin != *next)
- {
- *out++ = *begin++;
- ++next;
- }
- else
- {
- advance(begin,2);
- advance(next,2);
- }
- }
- }
- int main()
- {
- std::vector<int> arr(20);
- std::vector<int> arr1(25);
- std::vector<int> arrSet(20);
- std::vector<int> arr1Set(25);
- randomize(arr.begin(),arr.end(),[](){return rand()%30+10;});
- randomize(arr1.begin(),arr1.end(),[](){return rand()%30+10;});
- std::copy(arr.begin(),arr.end(),arrSet.begin());
- std::copy(arr1.begin(),arr1.end(),arr1Set.begin());
- print(arr.begin(),arr.end());
- print(arr1.begin(),arr1.end());
- std::cout << '\n';
- std::sort(arr.begin(),arr.end());
- std::sort(arr1.begin(),arr1.end());
- std::cout << "\nSorted containers\n";
- print(arr.begin(),arr.end());
- print(arr1.begin(),arr1.end());
- std::cout << '\n';
- auto uniqeIt{std::unique(arr.begin(),arr.end())};
- auto uniqeIt1{std::unique(arr1.begin(),arr1.end())};
- std::cout << "\nUniqued, but not erased containers\n";
- print(arr.begin(),arr.end());
- print(arr1.begin(),arr1.end());
- std::cout << '\n';
- arr.erase(uniqeIt,arr.end());
- arr1.erase(uniqeIt1,arr1.end());
- std::cout << "\nUniqued containers\n";
- print(arr.begin(),arr.end());
- print(arr1.begin(),arr1.end());
- std::cout << '\n';
- auto isGood = [&arr1](int el){return !(std::binary_search(arr1.begin(), arr1.end(), el));};
- auto isGood1 = [&arr](int el){return !(std::binary_search(arr.begin(), arr.end(), el));};
- std::vector<int> res;
- std::copy_if(arr.begin(),arr.end(),std::inserter(res,res.end()),isGood);
- std::copy_if(arr1.begin(),arr1.end(),std::inserter(res,res.end()),isGood1);
- std::sort(res.begin(),res.end());
- std::cout << "\nResult with sort+unique+binary_search\n";
- print(res.begin(),res.end());
- std::cout << "\nAlternative method\n";
- std::vector<int> join(arr.size() + arr1.size());
- std::merge(arr.begin(),arr.end(),arr1.begin(),arr1.end(),join.begin());
- print(join.begin(),join.end());
- std::cout << "\nAlternative result\n";
- std::vector<int> resAlt;
- dedup(join.begin(), join.end(), std::inserter(resAlt,resAlt.end()));
- print(resAlt.begin(),resAlt.end());
- std::cout << "\nSame task using std::set\n";
- std::set<int> resSet;
- std::set<int> res1Set;
- for(auto& el: arrSet)
- {
- resSet.insert(el);
- }
- for(auto& el: arr1Set)
- {
- res1Set.insert(el);
- }
- std::vector<int> result;
- std::set_difference(resSet.begin(),resSet.end(), res1Set.begin(),res1Set.end(), std::inserter(result,result.end()));
- std::set_difference(res1Set.begin(),res1Set.end(), resSet.begin(),resSet.end(), std::inserter(result,result.end()));
- std::sort(result.begin(),result.end());
- print(result.begin(), result.end());
- std::cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement