C++ std::list swap() Function

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

Definition

In C++, std::list::swap() exchanges the whole content of two lists. After the call, each list holds what the other one held before.

The lists only swap their internal links, so no element is copied or moved, and the cost does not depend on the size. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call swap on one list and give the other list as the argument. You can also use std::swap.

C++
a.swap(b);          // member function
std::swap(a, b);    // same effect
CallWhat it does
a.swap(b)Exchanges the content of a and b. Both must be lists of the same type.
std::swap(a, b)The free function. It simply calls a.swap(b).

Swapping with an empty list is a quick way to take all elements out of a list: tmp.swap(big); leaves big empty.

Library declarations
C++
void swap(list& other) noexcept;   // member

template<class T, class Alloc>
void swap(list<T, Alloc>& a, list<T, Alloc>& b) noexcept;   // free function

Parameters

ParameterDescription
otherRequired. The other list to exchange with. It must have the same element type T.

Return Value

Return ValueDescription
Nothingswap() returns void. Both lists are changed.

C++ Examples

Example 1 — swap() Two Lists

Swap a list of three numbers with a list of one number.

C++
#include <iostream>
#include <list>

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

    a.swap(b);

    std::cout << "a:";
    for (int n : a) std::cout << ' ' << n;
    std::cout << "\nb:";
    for (int n : b) std::cout << ' ' << n;
    std::cout << '\n';
    return 0;
}

How It Works

  1. List a starts with 1 2 3 and list b starts with 9.
  2. a.swap(b) exchanges the internal links of the two lists. No number is copied.
  3. Now a holds 9 and b holds 1 2 3, even though their sizes were different.

Example 2 — swap() and Iterators

An iterator keeps pointing at the same element, but that element now lives in the other list.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> a = {1, 2, 3};
    std::list<int> b = {9};
    auto it = a.begin();

    a.swap(b);

    std::cout << std::boolalpha;
    std::cout << "*it: " << *it << '\n';
    std::cout << "it is b.begin(): " << (it == b.begin()) << '\n';
    return 0;
}

How It Works

  1. it points at the first node of a, which holds 1.
  2. The swap moves the whole chain of nodes to b. The node with 1 itself does not move in memory.
  3. So *it is still 1, and it equals b.begin(). The iterator followed the data into the other list.

Example 3 — swap() with an Empty List

Use an empty helper list to take all elements out of another list in one quick step.

C++
#include <iostream>
#include <list>
#include <utility>

int main() {
    std::list<int> big = {4, 5, 6, 7};
    std::list<int> tmp;

    std::swap(big, tmp);

    std::cout << "big: " << big.size() << " elements\n";
    std::cout << "tmp: " << tmp.size() << " elements\n";
    return 0;
}

How It Works

  1. tmp starts empty and big holds four numbers.
  2. std::swap(big, tmp) calls the member swap(), which exchanges the links in constant time.
  3. Now big is empty and tmp owns all four nodes. No number was copied.

Frequently Asked Questions

It exchanges the whole content of two lists. After the call, each list holds the elements of the other list.
Yes. It only exchanges a few internal links and never copies elements, so the time does not depend on the size of the lists.
Yes. Iterators and references keep pointing at the same elements, but those elements now belong to the other list. The end() iterator is the exception, so call end() again after the swap.
No. Both lists must have the same type, for example std::list<int> with std::list<int>.
They do the same thing. std::swap(a, b) just calls a.swap(b) for std::list.

Next: unique()

Erase repeated neighbouring elements with std::list.

unique() 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