C++ std::set_union() Function

Definition
In C++, std::set_union() writes every element that appears in at least one of two sorted ranges into a destination. The result is sorted.
Elements that appear in both ranges are written once. When the same value appears several times, the larger count of the two ranges is used.
Syntax
Add #include <algorithm>, then call std::set_union(). Start with these two lines.
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::set_union(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);
| Call | What it does |
|---|
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out)); | Union of two sorted vectors. |
std::set_union(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp); | Use your own ordering. |
Library declarations
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first);
Parameters
| Parameter | Description |
|---|
| first1, last1 | Required. The first sorted range. |
| first2, last2 | Required. The second sorted range. |
| d_first | Required. Where the result is written. |
| comp | Optional. The comparison both ranges were sorted with. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written in the destination. |
C++ Examples
Example 1 — Union of two lists
Combine two sorted vectors without repeating shared values.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3, 5};
std::vector<int> b = {3, 4, 5, 6};
std::vector<int> out;
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- 3 and 5 appear in both vectors.
- In the union each shared value is written only once.
- The result stays sorted.
Example 2 — Repeated values
See how repeated values are counted.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 2, 3};
std::vector<int> b = {2, 2, 2, 4};
std::vector<int> out;
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
a has two 2s and b has three 2s.- The union keeps the larger count, which is three.
- That is why there are three 2s in the result.
Example 3 — Union of words
Combine two sorted lists of names.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main() {
std::vector<std::string> a = {"apple", "cherry"};
std::vector<std::string> b = {"banana", "cherry"};
std::vector<std::string> out;
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
for (const std::string& x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- Both lists are in alphabetical order.
cherry is in both lists, so it appears once.- The words come out in alphabetical order.
Frequently Asked Questions
In C++, std::set_union() builds the union of two sorted ranges. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the position after the last element written in the destination.
merge() keeps all elements from both ranges. set_union() writes shared elements only once.
Yes. The algorithm assumes sorted input.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful