C++ std::set_union
The algorithm std::set_union
function computes the union of two sorted ranges and stores the result in a destination range. The result contains all elements from both ranges, with duplicate elements appearing only once. Both input ranges must be sorted according to the same criteria for the function to work correctly.
Syntax of std::set_union
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
Parameters of std::set_union
Parameter | Description |
---|---|
first1, last1 | Input iterators defining the first sorted range. |
first2, last2 | Input iterators defining the second sorted range. |
result | Output iterator to the beginning of the destination range where the result is stored. |
comp (optional) | A binary comparison function that defines the order of elements. Defaults to < . |
Return Value of std::set_union
Returns an iterator to the end of the destination range containing the union of the two input ranges.
Examples for std::set_union
Example 1: Basic Usage of std::set_union
This example demonstrates finding the union of two sorted vectors:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> set1 = {1, 3, 5, 7};
std::vector<int> set2 = {2, 3, 6, 7};
std::vector<int> result(set1.size() + set2.size());
auto it = std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin());
result.resize(it - result.begin()); // Adjust size to remove unused elements
std::cout << "Union of sets: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Include necessary headers:
<iostream>
: Used for input/output operations.<algorithm>
: Includes thestd::set_union
function.<vector>
: Used for thestd::vector
container.
- Initialize two sorted sets:
set1
: A vector containing{1, 3, 5, 7}
.set2
: A vector containing{2, 3, 6, 7}
.- Both vectors must be sorted in ascending order for
std::set_union
to work correctly.
- Prepare a result vector:
- A vector named
result
is initialized with a size large enough to hold all possible elements from both sets, which is the sum of their sizes.
- A vector named
- Perform the union operation:
- The function
std::set_union
merges the two sorted rangesset1
andset2
intoresult
, ensuring no duplicate elements are included in the union. - It takes five arguments:
set1.begin()
: Iterator pointing to the beginning ofset1
.set1.end()
: Iterator pointing to the end ofset1
.set2.begin()
: Iterator pointing to the beginning ofset2
.set2.end()
: Iterator pointing to the end ofset2
.result.begin()
: Iterator pointing to the beginning of the result vector, where the merged elements will be stored.
- The function returns an iterator pointing to the end of the resulting range.
- The function
- Resize the result vector:
- The size of the
result
vector is adjusted to remove unused elements usingresult.resize(it - result.begin())
.
- The size of the
- Output the union of the sets:
- The program uses a for loop to iterate over the elements in the
result
vector and prints them separated by spaces. - The output is:
"Union of sets: 1 2 3 5 6 7"
.
- The program uses a for loop to iterate over the elements in the
Output:
Union of sets: 1 2 3 5 6 7
Example 2: Using a Custom Comparison Function for std::set_union
This example demonstrates finding the union of two sorted ranges in descending order using a custom comparison function:
#include <iostream>
#include <algorithm>
#include <vector>
bool descending(int a, int b) {
return a > b;
}
int main() {
std::vector<int> set1 = {7, 5, 3, 1};
std::vector<int> set2 = {7, 6, 3, 2};
std::vector<int> result(set1.size() + set2.size());
auto it = std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin(), descending);
result.resize(it - result.begin()); // Adjust size to remove unused elements
std::cout << "Union of sets in descending order: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Union of sets in descending order: 7 6 5 3 2 1
Exception Handling in std::set_union
The std::set_union
function does not throw exceptions on its own. However, the comparison function passed as an argument may throw exceptions, which can be caught and handled appropriately.
Example 1: Exception in Custom Comparison Function
This example demonstrates how exceptions in a custom comparison function are handled:
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdexcept>
bool faulty_compare(int a, int b) {
if (a == 3 || b == 3) {
throw std::runtime_error("Comparison involving 3 is not allowed.");
}
return a < b;
}
int main() {
std::vector<int> set1 = {1, 3, 5, 7};
std::vector<int> set2 = {2, 3, 6, 7};
std::vector<int> result(set1.size() + set2.size());
try {
auto it = std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin(), faulty_compare);
result.resize(it - result.begin()); // Adjust size to remove unused elements
std::cout << "Union of sets: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Output:
Exception caught: Comparison involving 3 is not allowed.