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
Call
What 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
Parameter
Description
other
Required. The other list to exchange with. It must have the same element type T.
Return Value
Return Value
Description
Nothing
swap() 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;
}
Output
a: 9
b: 1 2 3
How It Works
List a starts with 1 2 3 and list b starts with 9.
a.swap(b) exchanges the internal links of the two lists. No number is copied.
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;
}
Output
*it: 1
it is b.begin(): true
How It Works
it points at the first node of a, which holds 1.
The swap moves the whole chain of nodes to b. The node with 1 itself does not move in memory.
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.
std::swap(big, tmp) calls the member swap(), which exchanges the links in constant time.
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.