C++ std::set swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

Definition

In C++, std::set::swap() exchanges the contents of two sets. After the call, each set holds what the other one had.

It works in constant time, because only internal pointers are swapped. No value is copied or moved, and no value is destroyed. Both sets must have the same type. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call it on one set and pass the other. The free function std::swap does the same.

C++
std::set<int> a = {1, 2, 3};
std::set<int> b = {7, 8};
a.swap(b);              // a: 7 8   b: 1 2 3
std::swap(a, b);        // swap back
CallWhat it does
a.swap(b)Exchanges the contents of a and b. Returns nothing.
std::swap(a, b)The non-member version. It calls a.swap(b) for you.

Both sets must be the same type, including the comparison. After the swap, an iterator that pointed into a still points to the same value, but that value now lives in b. The end() iterators are the exception, because they still belong to their original sets.

Library declarations
C++
void swap(set& other) noexcept(/* see C++ standard */);
template<class Key, class Compare, class Alloc>
void swap(std::set<Key, Compare, Alloc>& a, std::set<Key, Compare, Alloc>& b);

Parameters

ParameterDescription
otherThe set to exchange contents with. It must have the same type as this set.

Return Value

Return ValueDescription
voidNothing is returned. Both sets are changed.

C++ Examples

Example 1 — swap() Exchanges Contents

Swap a three-value set with a two-value set and print both.

C++
#include <iostream>
#include <set>

void show(const char* label, const std::set<int>& s) {
    std::cout << label << ':';
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
}

int main() {
    std::set<int> a = {1, 2, 3};
    std::set<int> b = {7, 8};

    a.swap(b);

    show("a", a);
    show("b", b);
    return 0;
}

How It Works

  1. Before the call, a holds 1, 2, 3 and b holds 7, 8. The sets have different sizes.
  2. a.swap(b) swaps their internal trees. No value is copied, so it is fast even if the sets were huge.
  3. Now a has 7 8 and b has 1 2 3. Both are still sorted and still unique.

Example 2 — swap() and Iterators

An iterator saved before the swap still points at its value, but the value now sits in the other set.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> a = {1, 2, 3};
    std::set<int> b = {7, 8};

    auto it = a.find(2);
    a.swap(b);

    std::cout << std::boolalpha;
    std::cout << "*it: " << *it << '\n';
    std::cout << "a has 2: " << a.contains(2) << '\n';
    std::cout << "it is in b: " << (it == b.find(2)) << '\n';
    return 0;
}

How It Works

  1. it points to the value 2 in a before the swap. contains() needs C++20.
  2. The swap does not copy values, so it stays valid and *it still prints 2.
  3. The value 2 now lives in b, so a.contains(2) is false and it == b.find(2) is true. The iterator follows the value, not the set name.

Example 3 — swap() to Refresh a Set

Build the new data in a spare set, then swap it in. The old data comes out in the spare.

C++
#include <iostream>
#include <set>
#include <vector>

int main() {
    std::set<int> current = {1, 2, 3};
    std::vector<int> fresh = {8, 7, 8};

    std::set<int> next(fresh.begin(), fresh.end());
    current.swap(next);

    std::cout << "current:";
    for (int x : current) {
        std::cout << ' ' << x;
    }
    std::cout << "\nold data size: " << next.size() << '\n';
    return 0;
}

How It Works

  1. The new data is first built into a separate set called next. The repeated 8 is dropped, so it holds 7 and 8.
  2. current.swap(next) installs the new data in one fast step. If building next had failed, current would still be unchanged.
  3. current now holds 7 8. The old values 1, 2, 3 moved into next, so its size is 3. When next goes out of scope, the old data is freed.

Frequently Asked Questions

It exchanges all the values of two sets of the same type, in constant time and without copying.
No. It only swaps internal pointers, so it takes the same short time for a small set and a huge set.
Yes, except for end(). An iterator to a value still points to that value, but the value now lives in the other set.
No. Both sets must be the same type, and the comparison is part of the type.
They do the same work. std::swap(a, b) just calls a.swap(b) for sets.

Next: upper_bound()

Keep going through the std::set functions. Find the first value that is greater than the given value.

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

15 people found this page helpful