
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ unordered_multimap::swap() Function
The C++ std::unordered_multimap::swap() function is used to exchange the content of the first unordered_multimap with another unordered_multimap. It will takes place only if both the elements of the unordered_multimaps are of same type. This function does not depend on size.
Syntax
Following is the syntax of std::unordered_multimap::swap() function.
void swap(unordered_multimap<Key,T,Hash,Pred,Alloc>& first, unordered_multimap<Key,T,Hash,Pred,Alloc>& second);
Parameters
- first − First unordered_multimap object.
- second − Second unordered_multimap object of same type.
Return value
This function does not returns any things.
Example 1
Let's look at the following example, where we are going to perform the swap between two multimaps using the swap(() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'b', 4}, {'c', 5}, }; unordered_multimap<char, int> umm2 = { {'A', 1}, {'B', 2}, {'C', 3}, {'D', 4} }; cout << "umm1 contains following elements before swapping: " << endl; for (auto it = umm1.begin(); it != umm1.end(); ++it) cout << it->first << " = " << it->second << endl; cout << "umm2 contains following elements before swapping: " << endl; for (auto it = umm2.begin(); it != umm2.end(); ++it) cout << it->first << " = " << it->second << endl; swap(umm1, umm2); cout << "umm1 contains following elements after swapping: " << endl; for (auto it = umm1.begin(); it != umm1.end(); ++it) cout << it->first << " = " << it->second << endl; cout << "umm2 contains following elements after swapping: " << endl; for (auto it = umm2.begin(); it != umm2.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
If we run the above code it will generate the following output −
umm1 contains following elements before swapping: c = 5 c = 3 b = 4 b = 2 a = 1 umm2 contains following elements before swapping: D = 4 C = 3 B = 2 A = 1 umm1 contains following elements after swapping: D = 4 C = 3 B = 2 A = 1 umm2 contains following elements after swapping: c = 5 c = 3 b = 4 b = 2 a = 1
Example 2
Consider another scenario where we are going to create two multimaps, one with elements and the other empty, and apply the swap() function to swap elements into the empty multimap.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'b', 4}, {'c', 5}, }; unordered_multimap<char, int> umm2; swap(umm1, umm2); cout << "umm2 unordered multimap contains following elements" << endl; for (auto it = umm2.begin(); it != umm2.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Following is the output of the above code −
umm2 unordered multimap contains following elements c = 5 c = 3 b = 4 b = 2 a = 1
Example 3
In the following example, we are going to use the two multimaps and swap the elements of both the multimaps with eachother using the swap() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<string, int> John = { {"CHE", 85}, {"PHY", 88}, {"MAT", 99}, {"CMS", 86}, {"ENG", 95} }; unordered_multimap<string, int> Bob = { {"PHY", 90}, {"CHE", 82}, {"MAT", 98}, {"CMS", 88} }; cout<<"Swapping the marks of first unordered multimap with second unordered_multimap"<<endl; swap(John, Bob); cout <<"John contains following marks after swapping: " << endl; for (auto & it: John) cout << it.first << " = " << it.second << endl; cout <<"Bob contains following marks after swapping: " << endl; for (auto & it: Bob) cout << it.first << " = " << it.second << endl; return 0; }
Output
Output of the above code is as follows −
Swapping the marks of first unordered multimap with second unordered_multimap john contains following marks after swapping: CMS = 88 MAT = 98 CHE = 82 PHY = 90 Bob contains following marks after swapping: ENG = 95 CMS = 86 MAT = 99 PHY = 88 CHE = 85