
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
unordered_multimap swap() function in C++ STL
unordered_multimap swap() function in C++ STL is used to swap the elements of one multimap to another of same size and type.
Algorithm
Begin Declaring two empty map container m, m1. Insert some values in both m, m1 map containers. Perform swap() function to swap the values of m, m1 map containers. Printing the swapped values of m map container. Printing the swapped values of m1 map container. End.
Example Code
#include<iostream> #include <bits/stdc++.h> using namespace std; int main() { unordered_map<char, int> m,m1; // declaring m, m1 as empty map container m.insert (pair<char, int>('b', 10)); //inserting values in map container m.insert (pair<char, int>('c',30)); m.insert (pair<char, int>('d',40)); m1.insert (pair<char, int>('a', 20)); m1.insert (pair<char, int>('e',70)); m1.insert (pair<char, int>('f',60)); m.swap(m1); // swapping the values of two map container. cout << "\n Key and values of 1st set are:"; for (auto it = m.begin(); it != m.end(); it++) { cout << "{" << it->first << ", " << it->second << "} "; // printing the swapped values of m map container } cout << "\n Key and values of 2nd set are:"; // printing the swapped values of m1 map container for (auto it = m1.begin(); it != m1.end(); it++) { cout << "{" << it->first << ", " << it->second << "} "; } return 0; }
Output
Key and values of 1st set are: {f, 60} {a, 20} {e, 70} Key and values of 2nd set are: {d, 40} {b, 10} {c, 30}.
- Related Articles
- unordered_multimap rehash() function in C++ STL
- unordered_multimap reserve() function in C++ STL
- unordered_multimap size() function in C++ STL
- multimap swap() function in C++ STL
- forward_list::swap( ) in C++ STL
- list swap( ) in C++ STL
- queue::swap() in C++ STL
- multimap::swap() in C++ STL
- stack swap() in C++ STL
- swap() function in C++
- Array::fill() and array::swap() in C++ STL?
- deque::at() and deque::swap() in C++ STL
- map::at() and map::swap() in C++ STL
- deque::at() and deque::swap() in C++ programming STL
- Unordered_multimap operator= in C++

Advertisements