C++ std::set_symmetric_difference() Function

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

Definition

In C++, std::set_symmetric_difference() writes the elements that are in one sorted range or the other, but not in both.

Think of it as the union without the shared part. The order of the two ranges does not matter.

Syntax

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

C++
std::set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);
CallWhat it does
std::set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));Keep elements found in only one range.
std::set_symmetric_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_symmetric_difference(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 — Items in only one list

Find what the two vectors do not share.

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

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

    std::set_symmetric_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. 3 and 4 appear in both vectors, so they are left out.
  2. 1 and 2 are only in a. 5 and 6 are only in b.
  3. The result holds all four, in sorted order.

Example 2 — Repeated values

Only the extra copies are kept.

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

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

    std::set_symmetric_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. a has two 2s and b has one. One pair cancels.
  2. One extra 2 from a is left.
  3. With 1 and 3 that gives 1, 2 and 3.

Example 3 — Equal ranges

Identical data leaves nothing.

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

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

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

    if (out.empty()) {
        std::cout << "No differences\n";
    }
    return 0;
}

How It Works

  1. Every element of a has a partner in b.
  2. All pairs cancel, so nothing is written.
  3. out.empty() is true.

Frequently Asked Questions

In C++, std::set_symmetric_difference() builds the symmetric difference of two sorted ranges: elements in exactly one of them. 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_difference() keeps what is only in the first range. The symmetric version keeps what is only in either range.
Nothing is written, so the destination stays empty.

Next: set_union()

Builds the union of two sorted ranges.

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

22 people found this page helpful