Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- template <class InIter>
- InIter dup_only(InIter begin, InIter end)
- {
- InIter out{ begin };
- while (begin != end and std::next(begin) != end)
- {
- if (*begin == *std::next(begin))
- *out++ = std::move(*begin++);
- if (begin != end)
- ++begin;
- }
- return out;
- }
- template <class InIter>
- InIter dedup(InIter begin, InIter end)
- {
- InIter out{ begin };
- while (begin != end and std::next(begin) != end)
- {
- if (*begin != *std::next(begin))
- *out++ = std::move(*begin++);
- else
- if (++begin != end)
- ++begin;
- }
- if (begin != end)
- *out++ = std::move(*begin);
- return out;
- }
- int main()
- {
- char input{ 'x' };
- std::vector<int> arrA{ 6,1,4,2,8,9,11,26,2,7,8,11,2,2,3 };
- std::vector<int> arrB{ 1,8,29,2,2,9,11,1,2,5,8,34,12,9,8 };
- std::sort(arrA.begin(), arrA.end());
- std::sort(arrB.begin(), arrB.end());
- std::copy(arrA.begin(), arrA.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::copy(arrB.begin(), arrB.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::cout << '\n';
- auto aUnique{ std::unique(arrA.begin(), arrA.end()) };
- auto bUnique{ std::unique(arrB.begin(), arrB.end()) };
- std::copy(arrA.begin(), aUnique, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::copy(arrB.begin(), bUnique, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::cout << '\n';
- do
- {
- std::cout << "\na - Only different elements from A and B without duplicates\n";
- std::cout << "b - Only common elements from A and B without duplicates\n";
- std::cout << "c - Only elements from A that not in B without duplicates\n";
- std::cout << "d - Only elements from B that not in A without duplicates\n";
- std::cin >> input; input = tolower(input);
- std::cout << '\n';
- if (input == 'a')
- {
- std::vector<int> res(std::distance(arrA.begin(), aUnique) + std::distance(arrB.begin(), bUnique));
- std::merge(arrA.begin(), aUnique, arrB.begin(), bUnique, res.begin());
- std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::cout << '\n';
- auto resEnd{ dedup(res.begin(), res.end()) };
- std::copy(res.begin(), resEnd, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- }
- else if (input == 'b')
- {
- std::vector<int> res(std::distance(arrA.begin(), aUnique) + std::distance(arrB.begin(), bUnique));
- std::merge(arrA.begin(), aUnique, arrB.begin(), bUnique, res.begin());
- std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- std::cout << '\n';
- auto resEnd{ dup_only(res.begin(), res.end()) };
- std::copy(res.begin(), resEnd, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n';
- }
- else if (input == 'c')
- {
- auto aEnd{ std::copy_if(arrA.begin(), aUnique, arrA.begin(),
- [&arrB](const auto& el) { return !(std::binary_search(arrB.begin(), arrB.end(), el)); }) };
- std::copy(arrA.begin(), aEnd, std::ostream_iterator<int>(std::cout, " "));
- std::cout << '\n';
- }
- else if (input == 'd')
- {
- auto bEnd{ std::copy_if(arrB.begin(), bUnique, arrB.begin(),
- [&arrA](const auto& el) { return !(std::binary_search(arrA.begin(), arrA.end(), el)); }) };
- std::copy(arrB.begin(), bEnd, std::ostream_iterator<int>(std::cout, " "));
- std::cout << '\n';
- }
- } while (input != 'x');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement