How to Find Union of Two Sets in C++: Simple Guide
To find the union of two sets in C++, use the
std::set_union algorithm from the algorithm header. It merges two sorted ranges into a destination container, producing a sorted union without duplicates.Syntax
The std::set_union function merges two sorted ranges into a destination range, producing their union. It requires the input ranges to be sorted.
first1, last1: Iterators defining the first sorted range.first2, last2: Iterators defining the second sorted range.result: Iterator to the beginning of the destination range where the union will be stored.
The function returns an iterator to the end of the constructed union range.
cpp
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt result);
Example
This example shows how to find the union of two std::set containers using std::set_union. The result is stored in a std::vector and printed.
cpp
#include <iostream> #include <set> #include <vector> #include <algorithm> int main() { std::set<int> set1 = {1, 3, 5, 7}; std::set<int> set2 = {3, 4, 5, 6}; std::vector<int> union_result; union_result.resize(set1.size() + set2.size()); auto it = std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), union_result.begin()); union_result.resize(it - union_result.begin()); std::cout << "Union of sets: "; for (int num : union_result) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output
Union of sets: 1 3 4 5 6 7
Common Pitfalls
Common mistakes when finding the union of two sets include:
- Not ensuring the input ranges are sorted before calling
std::set_union. This leads to incorrect results. - Not resizing the destination container properly before or after the operation, which can cause undefined behavior or extra unwanted elements.
- Using containers without sorted order (like
std::unordered_set) directly withstd::set_union.
cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v1 = {3, 1, 7}; // Not sorted std::vector<int> v2 = {4, 5, 6}; // Sorted std::vector<int> result(10); // Wrong: input ranges not sorted auto it = std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin()); result.resize(it - result.begin()); std::cout << "Wrong union result: "; for (int n : result) std::cout << n << " "; std::cout << std::endl; // Correct: sort first std::sort(v1.begin(), v1.end()); result.resize(10); it = std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin()); result.resize(it - result.begin()); std::cout << "Correct union result: "; for (int n : result) std::cout << n << " "; std::cout << std::endl; return 0; }
Output
Wrong union result: 3 1 4 5 6
Correct union result: 1 3 4 5 6 7
Quick Reference
- Use
std::set_unionfor sorted containers. - Ensure input ranges are sorted before calling.
- Resize the output container after the operation.
- Use
std::setor sortedstd::vectoras inputs.
Key Takeaways
Use std::set_union to find the union of two sorted sets in C++.
Always ensure input ranges are sorted before using std::set_union.
Resize the output container after the union operation to fit the result.
std::set containers are naturally sorted and ideal for set operations.
Avoid using unsorted containers directly with std::set_union to prevent errors.