C++ std::set_difference() Function

Definition
In C++, std::set_difference() writes the elements that are in the first sorted range but not in the second. The result is sorted.
Order matters. A minus B is different from B minus A.
Syntax
Add #include <algorithm>, then call std::set_difference(). Start with these two lines.
std::set_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::set_difference(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);
| Call | What it does |
|---|
std::set_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out)); | Elements in a that are not in b. |
std::set_difference(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_difference(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first);
Parameters
| Parameter | Description |
|---|
| first1, last1 | Required. The sorted range to take elements from. |
| first2, last2 | Required. The sorted range of elements to leave out. |
| 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 — Remove some values
Take 2 and 4 out of a list.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3, 4, 5};
std::vector<int> b = {2, 4};
std::vector<int> out;
std::set_difference(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
- The function copies elements of
a that have no partner in b. - 2 and 4 have a match in
b, so they are skipped. - 1, 3 and 5 are written.
Example 2 — Repeated values
Each element in b cancels only one copy in a.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 2, 2, 3};
std::vector<int> b = {2};
std::vector<int> out;
std::set_difference(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
b has only one 2, so it cancels only one 2 from a.- Two 2s are left over.
- The result is 1, 2, 2 and 3.
Example 3 — Order matters
Compare A minus B with B minus A.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {2, 3, 4};
std::vector<int> ab;
std::vector<int> ba;
std::set_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(ab));
std::set_difference(b.begin(), b.end(), a.begin(), a.end(), std::back_inserter(ba));
std::cout << "A - B: ";
for (int x : ab) std::cout << x << ' ';
std::cout << '\n';
std::cout << "B - A: ";
for (int x : ba) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The first call keeps what only
a has: 1. - The second call swaps the roles and keeps what only
b has: 4. - The two answers are different, so the argument order matters.
Frequently Asked Questions
In C++, std::set_difference() builds the difference of two sorted ranges: elements in the first but not the second. 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.
No. The first keeps elements only in a. The second keeps elements only in b.
set_symmetric_difference() keeps elements that are in only one of the two ranges, from either side.
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.
23 people found this page helpful