multimap swap() function in C++ STL
The multimap::swap() is a built-in function in C++ STL which swaps two multimap container. The contents of multimap1 are in multimap2 and contents of multimap2 are in multimap1 after swap() function is called.
Syntax:
multimap1_name.swap(multimap2_name)
Parameters: This function accepts one parameter which is to be swapped with the multimap1_name,
Return Value: The function does not returns anything.
// C++ function for illustration // multiset::swap() function #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp1, mp2; // insert elements in random order mp1.insert({ 2, 30 }); mp1.insert({ 1, 40 }); mp2.insert({ 10, 60 }); mp2.insert({ 9, 20 }); cout << "\nThe multimap1 before applying swap() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } cout << "\nThe multimap2 before applying swap() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp2.begin(); itr != mp2.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // performs swap operation of two multimap mp1.swap(mp2); cout << "\nThe multimap1 after applying swap() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } cout << "\nThe multimap2 after applying swap() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp2.begin(); itr != mp2.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } |
chevron_right
filter_none
Recommended Posts:
- multimap::swap() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap upper_bound() function in C++ STL
- multimap empty() function in C++ STL
- multimap value_comp() function in C++ STL
- multimap clear() function in C++ STL
- multimap get_allocator() function in C++ STL
- multimap size() function in C++ STL
- multimap lower_bound() function in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- unordered_set swap() function in C++ STL
- unordered_multiset swap() function in C++ STL
- unordered_multimap swap() function in C++ STL
- valarray swap() function in c++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



