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