Merge two sets using std::set_union and std::merge in C++
This post will discuss how to merge elements of the two given sets into a new set container in C++. Since all elements in a set are distinct, the common elements in both input sequences are discarded in the destination set.
1. Using std::set::insert function
We can use the public member function std::set::insert of the std::unordered_set or std::set container for inserting elements from a set to another set.
Here’s a C++11 version that uses a copy constructor for copying elements of the first set into the result set and then calls insert() on the result set to insert elements of the second set. We can easily modify code to avoid copy constructor and use only insert() to insert elements of both the sets.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <set> #include <algorithm> int main() { std::set<int> odd = { 1, 3, 5 }; std::set<int> even = { 2, 4, 6 }; std::set<int> s(odd); s.insert(even.begin(), even.end()); for (auto const &e: s) { std::cout << e << ' '; } return 0; } |
Output:
1 2 3 4 5 6
2. Using std::set_union function
Another good alternative is to use the std::set_union that performs union operation on two sorted sequences. It takes input iterators to the initial and final positions of both sequences and output iterator to the initial position of the destination range. The specified comparator decides the ordering of elements in the destination range. If no comparator is specified, the elements are compared using default operator<.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <set> #include <algorithm> int main() { std::set<int> odd = { 1, 3, 5 }; std::set<int> even = { 2, 4, 6 }; std::set<int> s; std::set_union(odd.begin(), odd.end(), even.begin(), even.end(), std::inserter(s, s.begin())); for (auto const &e: s) { std::cout << e << ' '; } return 0; } |
Output:
1 2 3 4 5 6
3. Using std::merge function
The recommended approach uses the std::merge that combines the elements of two sorted sequences according to the specified comparator or operator<. Like std::set_union, it takes input iterators to the initial and final positions of input sequences and output iterator to the initial position of the destination sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <set> #include <algorithm> int main() { std::set<int> odd = { 1, 3, 5 }; std::set<int> even = { 2, 4, 6 }; std::set<int> s; std::merge(odd.begin(), odd.end(), even.begin(), even.end(), std::inserter(s, s.begin())); for (auto const &e: s) { std::cout << e << ' '; } return 0; } |
Output:
1 2 3 4 5 6
That’s all about merging two sets using std::set_union and std::merge in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)