C++ std::set_intersection() Function

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.
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);
| Call | What 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
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_intersection(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 — Shared numbers
Find the numbers that are in both vectors.
#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
- 3, 4 and 5 appear in both vectors.
- 1 and 2 are only in
a, and 6 and 7 are only in b. - Only the shared values are written.
Example 2 — Repeated values
The smaller count wins.
#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
a has three 2s and b has two.- Only two 2s can be matched in both ranges.
- So the result has two 2s.
Example 3 — Nothing in common
Two lists with no shared values.
#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
- No value from
a is in b. - Nothing is written to
out. - 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.
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.
16 people found this page helpful