C++ unordered_map::swap() 函式



C++ 的 std::unordered_map::swap() 函式用於交換第一個 unordered_map 與另一個 unordered_map 的內容。當另一個容器型別相同的時候,交換才會發生。

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

語法

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

void swap(unordered_map& ump);

引數

  • ump − 指示要交換的容器。

返回值

此函式不返回任何值。

示例 1

在下面的示例中,讓我們看看 swap() 函式的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um1 = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'e', 5}
   };
   unordered_map<char, int> um2;
   um1.swap(um2);
   cout << "Unordered map contains following elements" << endl;
   for (auto it = um2.begin(); it != um2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Unordered map contains following elements
e = 5
a = 1
b = 2
c = 3
d = 4

示例 2

在下面的示例中,我們將執行兩個容器之間的交換,並將它們的值互換。

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

輸出

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

um1 contains following element before swap: 
d = 4
c = 3
b = 2
a = 1
um2 contains following element before swap: 
h = 8
g = 7
f = 6
e = 5
um1 contains following elements after swap: 
h = 8
g = 7
f = 6
e = 5
um2 contains following elements after swap: 
d = 4
c = 3
b = 2
a = 1

示例 3

考慮下面的示例,我們將建立一個兩個對映,一個使用 [] 運算子,另一個正常建立,然後交換它們的值。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, int> um1;
   um1["Vivek"] = 80;
   um1["Aman"] = 85;
   um1["Akash"] = 90;
   unordered_map<string, int> um2 = { {"Sarika", 65}, {"Revathi", 95}, {"Daniel", 100} };
   cout<<"um1 contains following element before swap: "<<endl;
   for(auto & it:um1){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   cout<<"um2 contains following element before swap: "<<endl;
   for(auto & it:um2){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   um1.swap(um2);
   cout << "um1 contains following elements after swap: " << endl;
   for (auto it = um1.begin(); it != um1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      cout << "um2 contains following elements after swap: " << endl;
   for (auto it = um2.begin(); it != um2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

輸出

上述程式碼的輸出如下:

um1 contains following element before swap: 
Akash = 90
Aman = 85
Vivek = 80
um2 contains following element before swap: 
Revathi = 95
Daniel = 100
Sarika = 65
um1 contains following elements after swap: 
Revathi = 95
Daniel = 100
Sarika = 65
um2 contains following elements after swap: 
Akash = 90
Aman = 85
Vivek = 80
廣告