C++ std::set_union() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
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);
CallWhat 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
C++
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
                   InputIt2 first2, InputIt2 last2,
                   OutputIt d_first);

Parameters

ParameterDescription
first1, last1Required. The first sorted range.
first2, last2Required. The second sorted range.
d_firstRequired. Where the result is written.
compOptional. The comparison both ranges were sorted with.

Return Value

Return ValueDescription
IteratorPoints 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.

C++
#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

  1. 3 and 5 appear in both vectors.
  2. In the union each shared value is written only once.
  3. The result stays sorted.

Example 2 — Repeated values

See how repeated values are counted.

C++
#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

  1. a has two 2s and b has three 2s.
  2. The union keeps the larger count, which is three.
  3. That is why there are three 2s in the result.

Example 3 — Union of words

Combine two sorted lists of names.

C++
#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

  1. Both lists are in alphabetical order.
  2. cherry is in both lists, so it appears once.
  3. 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.

Next: shift_left()

Moves the elements of a range toward the front by n places.

shift_left() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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