C++multimap swap()函数用于交换(或交换)两个multimap的内容,但两个multimap必须为相同类型,尽管大小可能有所不同。
void swap (multimap& x);
x : 用于与其交换内容的multimap集合。
让我们看一下将一个multimap的元素交换到另一个的简单示例:
#include <iostream> #include <map> using namespace std; int main(void) { multimap<char, int> m1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'b', 4}, {'c', 5}, }; multimap<char, int> m2; m2.swap(m1); cout << "Multimap m2 contains following elements" << endl; for (auto it = m2.begin(); it != m2.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
输出:
Multimap m2 contains following elements a = 1 b = 2 b = 4 c = 3 c = 5
在上面的示例中,multimapm1具有五个元素,而m2为空。当您将m1交换为m2时,m1的所有元素都将交换为m2。
让我们看一个简单的示例来交换两个multimap的内容:
#include <iostream> #include <map> using namespace std; int main () { multimap<char,int> multimap1,multimap2; multimap1 = { {'x', 100}, {'x', 200} }; multimap2 = { {'a', 110}, {'b', 220}, {'a', 330} }; multimap1.swap(multimap2); cout << "multimap1 contains:\n"; for (multimap<char,int>::iterator it=multimap1.begin(); it!=multimap1.end(); ++it) cout << it->first << " => " << it->second << '\n'; cout << "multimap2 contains:\n"; for (multimap<char,int>::iterator it=multimap2.begin(); it!=multimap2.end(); ++it) cout << it->first << " => " << it->second << '\n'; return 0; }
输出:
multimap1 contains: a => 110 a => 330 b => 220 multimap2 contains: x => 100 x => 200
在上面的例子中,两个multimap的内容,即multimap1和multimap2相互交换。
让我们看一个简单的示例来交换两个multimap的内容:
#include<iostream> #include<map> using namespace std; int main() { //Take any two multimaps multimap<int, char> multimap1, multimap2; multimap1 = { {1, 'a'}, {2, 'b'}, {2, 'c'}, {4, 'd'} }; multimap2 = { {1, 'x'}, {2, 'y'}, {2, 'z'} }; //Swap elements of multimaps swap(multimap1, multimap2); //Print the elements of multimaps cout << "multimap1:\n"<< "\tKEY\tELEMENT\n"; for (auto it = multimap1.begin(); it != multimap1.end(); it++) cout << "\t" << it->first << "\t" << it->second << '\n'; cout << "multimap2:\n"<< "\tKEY\tELEMENT\n"; for (auto it = multimap2.begin(); it != multimap2.end(); it++) cout << "\t" << it->first << "\t" << it->second << '\n'; return 0; }
输出:
multimap1: KEY ELEMENT 1 x 2 y 2 z multimap2: KEY ELEMENT 1 a 2 b 2 c 4 d
在上面的示例中,另一种形式的swap()函数用于交换两个multimap的内容。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-swap-function.html
来源:LearnFk无涯教程网
让我们看一个简单的例子:
#include <iostream> #include <string> #include <map> using namespace std; void show(const char *msg, multimap<string, int> mp); int main() { multimap<string, int> m1, m2; m1.insert(pair<string, int>("A", 100)); m1.insert(pair<string, int>("B", 300)); m1.insert(pair<string, int>("B", 200)); //Exchange the contents of m1 and m2. cout << "Exchange m1 and m2.\n"; m1.swap(m2); show("Contents of m2: ", m2); show("Contents of m1: ", m1); //Clear m1. m1.clear(); if(m1.empty()) cout << "m1 is now empty."; return 0; } // Display the contents of a multimap<string, int> by using an iterator. void show(const char *msg, multimap<string, int> mp) { multimap<string, int>::iterator itr; cout << msg << endl; for(itr=mp.begin(); itr != mp.end(); ++itr) cout << " " << itr->first << ", " << itr->second << endl; cout << endl; }
输出:
Exchange m1 and m2. Contents of m2: A, 100 B, 300 B, 200 Contents of m1: m1 is now empty.
在上面的示例中,将multimapm1的内容交换到multimapm2,并且在清除了交换m1后,multimap已被清除。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
HarmonyOS快速入门与实战 -〔QCon+案例研习社〕