C++ std::set_difference() Function

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

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.

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

Parameters

ParameterDescription
first1, last1Required. The sorted range to take elements from.
first2, last2Required. The sorted range of elements to leave out.
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 — Remove some values

Take 2 and 4 out of a list.

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

  1. The function copies elements of a that have no partner in b.
  2. 2 and 4 have a match in b, so they are skipped.
  3. 1, 3 and 5 are written.

Example 2 — Repeated values

Each element in b cancels only one copy in a.

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

  1. b has only one 2, so it cancels only one 2 from a.
  2. Two 2s are left over.
  3. The result is 1, 2, 2 and 3.

Example 3 — Order matters

Compare A minus B with B minus A.

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

  1. The first call keeps what only a has: 1.
  2. The second call swaps the roles and keeps what only b has: 4.
  3. 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.

Next: set_intersection()

Builds the intersection of two sorted ranges.

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

23 people found this page helpful