C++ unordered_multimap::swap() 函式



C++ 的std::unordered_multimap::swap()函式用於交換第一個unordered_multimap與另一個unordered_multimap的內容。當另一個容器與第一個容器型別相同時,交換操作才會進行。

此函式交換元素或鍵/值對,而無需實際對單個元素執行任何複製或移動操作,從而無論大小都能實現常數時間執行。

語法

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

void swap(unordered_multimap& n);

引數

  • n - 表示另一個unordered_multimap物件。

返回值

此函式不返回任何值。

示例 1

讓我們看下面的例子,我們將演示unordered_multimap::swap()函式的使用。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm1 = {
      {'a', 1}, {'a', 10}, {'a', 100}, {'a', 1000}, {'a', 10000} 
   };
   unordered_multimap<char, int> umm2;
   umm1.swap(umm2);
   cout << "second unordered_multimap contains following elements after swap " << endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

輸出

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

second unordered_multimap contains following elements after swap 
a = 10000
a = 1000
a = 100
a = 10
a = 1

示例 2

考慮下面的例子,我們將使用swap()函式將一個容器的元素交換到另一個容器,反之亦然。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'b', 20}, {'c', 30}, };
   unordered_multimap<char, int> umm2 = { {'e', 5}, {'f', 6}, {'g', 7}, {'h', 8} };
   cout<<"umm1 contains following element before swap: "<<endl;
   for(auto & it:umm1){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   cout<<"umm2 contains following element before swap: "<<endl;
   for(auto & it:umm2){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   umm1.swap(umm2);
   cout << "umm1 contains following elements after swap: " << endl;
   for (auto it = umm1.begin(); it != umm1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      cout << "umm2 contains following elements after swap: " << endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

輸出

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

umm1 contains following element before swap: 
c = 30
c = 3
b = 20
b = 2
a = 1
umm2 contains following element before swap: 
h = 8
g = 7
f = 6
e = 5
umm1 contains following elements after swap: 
h = 8
g = 7
f = 6
e = 5
umm2 contains following elements after swap: 
c = 30
c = 3
b = 20
b = 2
a = 1

示例 3

在下面的例子中,我們將使用swap()函式僅將一個unordered_multimap中唯一的偶數鍵及其值交換到另一個unordered_multimap。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<int, int> umm1, umm2, temp;
   umm1 = {{1, 10}, {2, 20}, {1, 100}, {2, 200}, {3, 300}, {4, 40}};
   cout<<"umm1 contains following element before swap: "<<endl;
   for(auto& it:umm1){
      cout<<it.first<<" = "<<it.second<<endl;
   }
   for(auto & it:umm1){
      if(it.first%2 == 0){
         temp.insert({it.first, it.second});
      }
   }
   umm2.swap(temp);
   cout<<"umm2 contains following element before swap: "<<endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

輸出

以上程式碼的輸出如下:

umm1 contains following element before swap: 
4 = 40
3 = 300
2 = 200
2 = 20
1 = 100
1 = 10
umm2 contains following element before swap: 
2 = 20
2 = 200
4 = 40
廣告