C++ std::set_intersection() Function

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

Definition

In C++, std::set_intersection() writes the elements that appear in both sorted ranges to a destination. The result is sorted.

When a value appears several times, the smaller count of the two ranges is used.

Syntax

Add #include <algorithm>, then call std::set_intersection(). Start with these two lines.

C++
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);
CallWhat it does
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));Keep only the shared elements.
std::set_intersection(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_intersection(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 — Shared numbers

Find the numbers that are in both vectors.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b = {3, 4, 5, 6, 7};
    std::vector<int> out;

    std::set_intersection(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, 4 and 5 appear in both vectors.
  2. 1 and 2 are only in a, and 6 and 7 are only in b.
  3. Only the shared values are written.

Example 2 — Repeated values

The smaller count wins.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 2, 2};
    std::vector<int> b = {2, 2, 3};
    std::vector<int> out;

    std::set_intersection(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 three 2s and b has two.
  2. Only two 2s can be matched in both ranges.
  3. So the result has two 2s.

Example 3 — Nothing in common

Two lists with no shared values.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1, 2};
    std::vector<int> b = {3, 4};
    std::vector<int> out;

    std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));

    std::cout << "Common count: " << out.size() << '\n';
    return 0;
}

How It Works

  1. No value from a is in b.
  2. Nothing is written to out.
  3. The size of out is 0.

Frequently Asked Questions

In C++, std::set_intersection() builds the intersection 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.
set_union() keeps elements that are in either range. set_intersection() keeps only elements that are in both.
Yes. All the set algorithms work on sorted ranges.

Next: set_symmetric_difference()

Builds the symmetric difference of two sorted ranges: elements in exactly one of them.

set_symmetric_difference() 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.

16 people found this page helpful