C++ unordered_multimap::swap() 函式



C++ 的std::unordered_multimap::swap()函式用於交換第一個unordered_multimap與另一個unordered_multimap的內容。只有當unordered_multimap的元素型別相同時,才會進行交換。此函式不依賴於大小。

語法

以下是std::unordered_multimap::swap()函式的語法。

void swap(unordered_multimap<Key,T,Hash,Pred,Alloc>& first, unordered_multimap<Key,T,Hash,Pred,Alloc>& second);

引數

  • first − 第一個unordered_multimap物件。
  • second − 第二個相同型別的unordered_multimap物件。

返回值

此函式不返回任何值。

示例 1

讓我們來看下面的例子,我們將使用swap()函式在兩個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 = {
      {'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;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

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

示例 2

考慮另一種情況,我們將建立兩個multimap,一個包含元素,另一個為空,並應用swap()函式將元素交換到空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;
}

輸出

以下是上述程式碼的輸出:

umm2 unordered multimap contains following elements
c = 5
c = 3
b = 4
b = 2
a = 1

示例 3

在下面的例子中,我們將使用兩個multimap,並使用swap()函式相互交換兩個multimap的元素。

#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;
}

輸出

上述程式碼的輸出如下:

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
廣告