unordered_map swap in C++ STL Last Updated : 14 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The std::unordered_map::swap() is a built in function in C++ STL which swaps the elements of a container to an other container. After the call of this function elements of the caller unordered_map will be elements of called unordered_map while elements of called unordered_map will be elements of caller unordered_map. Internally swapping of elements is not done only reference type of both unordered_map is changed. Syntax unordered_map.swap ( unordered_map& ump ) Return type : Return type of this function is void. Parameters : An other unordered_map with same type of elements. Complexity : Its complexity is constant. Example 1 CPP // C++ code to illustrate the method // unordered_map swap #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> sample1, sample2; // Map initialization sample1 = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } }; sample2 = { { 10, 11 }, { 12, 13 }, { 14, 15 }, { 26, 17 } }; // printing details before calling swap cout << " Elements of maps before swap \n"; cout << " Elements of first map are : \n"; for (auto& x : sample1) cout << x.first << " : " << x.second << endl; cout << " Elements of second map are : \n"; for (auto& x : sample2) cout << x.first << " : " << x.second << endl; // swapping sample1.swap(sample2); cout << " Elements of maps after swap \n"; cout << " Elements of first map are : \n"; for (auto& x : sample1) cout << x.first << " : " << x.second << endl; cout << " Elements of second map are : \n"; for (auto& x : sample2) cout << x.first << " : " << x.second << endl; return 0; } Output: Elements of maps before swap Elements of first map are : 5 : 8 4 : 6 3 : 4 2 : 2 Elements of second map are : 14 : 15 26 : 17 12 : 13 10 : 11 Elements of maps after swap Elements of first map are : 14 : 15 26 : 17 12 : 13 10 : 11 Elements of second map are : 5 : 8 4 : 6 3 : 4 2 : 2 Example 2 CPP // C++ code to illustrate the method // unordered_map swap #include <bits/stdc++.h> using namespace std; int main() { unordered_map<char, int> sample1, sample2; // Map initialization sample1 = { { 'a', 2 }, { 'b', 4 }, { 'c', 6 }, { 'd', 8 } }; sample2 = { { 'e', 11 }, { 'f', 13 }, { 'h', 15 } }; // printing details before calling swap cout << " Elements of maps before swap \n"; cout << " Elements of first map are : \n"; for (auto& x : sample1) cout << x.first << " : " << x.second << endl; cout << " Elements of second map are : \n"; for (auto& x : sample2) cout << x.first << " : " << x.second << endl; // swapping sample1.swap(sample2); cout << " Elements of maps after swap \n"; cout << " Elements of first map are : \n"; for (auto& x : sample1) cout << x.first << " : " << x.second << endl; cout << " Elements of second map are : \n"; for (auto& x : sample2) cout << x.first << " : " << x.second << endl; return 0; } Output: Elements of maps before swap Elements of first map are : d : 8 c : 6 b : 4 a : 2 Elements of second map are : h : 15 f : 13 e : 11 Elements of maps after swap Elements of first map are : h : 15 f : 13 e : 11 Elements of second map are : d : 8 c : 6 b : 4 a : 2 Note : Caller and called unordered_map both should contain same type of elements otherwise we will get compile time error. Comment More infoAdvertise with us Next Article unordered_multiset swap() in C++ STL A ankit15697 Follow Improve Article Tags : Technical Scripter C++ cpp-unordered_map cpp-unordered_map-functions Practice Tags : CPP Similar Reads Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not 7 min read unordered_set swap() in C++ STL The swap() method of âunordered_setâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_set object containing elements of the same type but the sizes may differ. After the ca 3 min read unordered_set swap() in C++ STL The swap() method of âunordered_setâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_set object containing elements of the same type but the sizes may differ. After the ca 3 min read unordered_multiset swap() in C++ STL The swap() method of âunordered_multisetâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_multiset object containing elements of the same type but the sizes may differ. Af 3 min read unordered_multiset swap() in C++ STL The swap() method of âunordered_multisetâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_multiset object containing elements of the same type but the sizes may differ. Af 3 min read Unordered Multimap in C++ STL In C++, the unordered_multimap is an unordered associative container that stores data in the form of key-value pairs. It is similar to unordered map, but it allows multiple elements with the same key. It provides fast insertion, deletion and search operations in O(1) time by using hashing.Example:C+ 7 min read Like