In C++, std::map::swap()exchanges everything between two maps of the same type. After the call, each map holds what the other one held before.
It is very fast, because only the internal links are swapped. No pair is copied, moved, or destroyed. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax
Call swap on one map and pass the other.
C++
a.swap(b); // member function
std::swap(a, b); // non-member function (same result)
Call
What it does
a.swap(b)
Exchanges the contents of a and b. Both maps must have the same type.
std::swap(a, b)
The non-member version. It simply calls a.swap(b).
Swapping is a common way to publish a freshly built map: build a new map on the side, then swap it with the old one in one quick step.
Library declarations
C++
void swap(map& other) noexcept(/* see below */);
// non-member:
template<class Key, class T, class Cmp, class Alloc>
void swap(map<Key, T, Cmp, Alloc>& lhs, map<Key, T, Cmp, Alloc>& rhs);
Parameters
Parameter
Description
other
The map to exchange contents with. It must have the same key, value, comparator, and allocator types.
Return Value
Return Value
Description
void
Nothing is returned. The two maps now hold each other's old content.
C++ Examples
Example 1 — swap() Exchanges Two Maps
Swap two small maps and print both.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> a = {{1, "one"}};
std::map<int, std::string> b = {{2, "two"}, {3, "three"}};
a.swap(b);
std::cout << "a has " << a.size() << " pairs, first key " << a.begin()->first << '\n';
std::cout << "b has " << b.size() << " pair, first key " << b.begin()->first << '\n';
return 0;
}
Output
a has 2 pairs, first key 2
b has 1 pair, first key 1
How It Works
Before the call, a holds key 1 and b holds keys 2 and 3.
a.swap(b) exchanges the whole contents. It only swaps internal links, so it is fast no matter how many pairs there are.
Now a holds the two pairs with first key 2, and b holds the single pair with key 1.
Example 2 — swap() and Iterators
An iterator taken before the swap now points into the other map.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> a = {{1, "one"}};
std::map<int, std::string> b = {{2, "two"}, {3, "three"}};
auto it = a.begin(); // points at key 1 inside a
a.swap(b); // that pair now lives in b
std::cout << std::boolalpha;
std::cout << "value: " << it->second << '\n';
std::cout << "it is b.begin(): " << (it == b.begin()) << '\n';
return 0;
}
Output
value: one
it is b.begin(): true
How It Works
it is taken from a before the swap. It points to the pair 1 -> "one".
The swap moves ownership of the whole tree, not the pairs. The pair is not copied, so it stays valid and still reads one.
That pair now belongs to b and is its only element, so it == b.begin() is true.
Example 3 — swap() to Replace a Map
Build a new map on the side and swap it in. The old data ends up in the temporary map.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> current = {{"old", 1}};
std::map<std::string, int> fresh;
fresh["new"] = 2;
fresh["newer"] = 3;
current.swap(fresh);
std::cout << "current has " << current.size() << " keys, new exists: "
<< std::boolalpha << current.contains("new") << '\n';
std::cout << "fresh holds the old data, size " << fresh.size() << '\n';
return 0;
}
Output
current has 2 keys, new exists: true
fresh holds the old data, size 1
How It Works
fresh is filled with the new data while current is still untouched. This can take as long as needed.
current.swap(fresh) replaces the content in one step. Readers of current never see a half-built map. (This example uses contains(), which needs C++20.)
After the swap, current holds two keys, and fresh holds the old single pair. When fresh goes out of scope, the old data is freed.
Frequently Asked Questions
It exchanges the entire contents of two maps of the same type, in constant time.
No. It only swaps internal pointers, so it does not copy, move, or destroy any pair.
No. They stay valid but now refer to elements in the other map. The end() iterators are the exception.
Yes. The non-member std::swap for maps just calls the member function.
No. Both maps need the same key, value, comparator, and allocator types.