C ++ STL中的unordered_multimap swap()函数
swap()C++STL中的unordered_multimap函数用于将一个多图的元素交换为相同大小和类型的另一个。
算法
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.
范例程式码
#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;
}输出结果
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}.热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿