当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_map swap用法及代码示例


std::unordered_map::swap()是C++ STL中的内置函数,可将容器的元素交换到另一个容器。调用此函数后,调用方unordered_map的元素将成为unordered_map的元素,而调用unordered_map的元素将成为unordered_map的元素。内部元素交换未完成,仅两个unordered_map的引用类型均已更改。句法

unordered_map.swap ( unordered_map& ump )

返回类型:此函数的返回类型为void。
参数:另一个unordered_map具有相同类型的元素。
复杂:它的复杂性是恒定的。

例子1



// C++ code to illustrate the method 
// unordered_map swap 
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
    unordered_map<int, int> sample1, sample2; 
  
    // Map initialization 
    sample1 = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } }; 
    sample2 = { { 10, 11 }, { 12, 13 }, { 14, 15 }, { 26, 17 } }; 
  
    // printing details before calling swap 
  
    cout << " Elements of maps before swap \n"; 
    cout << " Elements of first map are:\n"; 
    for (auto& x:sample1) 
        cout << x.first << ":" << x.second << endl; 
  
    cout << " Elements of second map are:\n"; 
    for (auto& x:sample2) 
        cout << x.first << ":" << x.second << endl; 
  
    // swapping 
    sample1.swap(sample2); 
  
    cout << " Elements of maps after swap \n"; 
    cout << " Elements of first map are:\n"; 
    for (auto& x:sample1) 
        cout << x.first << ":" << x.second << endl; 
  
    cout << " Elements of second map are:\n"; 
    for (auto& x:sample2) 
        cout << x.first << ":" << x.second << endl; 
    return 0; 
}
输出:
Elements of maps before swap 
 Elements of first map are:
5:8
4:6
3:4
2:2
 Elements of second map are:
14:15
26:17
12:13
10:11
 Elements of maps after swap 
 Elements of first map are:
14:15
26:17
12:13
10:11
 Elements of second map are:
5:8
4:6
3:4
2:2

例子2

// C++ code to illustrate the method 
// unordered_map swap 
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
    unordered_map<char, int> sample1, sample2; 
  
    // Map initialization 
    sample1 = { { 'a', 2 }, { 'b', 4 }, { 'c', 6 }, { 'd', 8 } }; 
    sample2 = { { 'e', 11 }, { 'f', 13 }, { 'h', 15 } }; 
  
    // printing details before calling swap 
  
    cout << " Elements of maps before swap \n"; 
    cout << " Elements of first map are:\n"; 
    for (auto& x:sample1) 
        cout << x.first << ":" << x.second << endl; 
  
    cout << " Elements of second map are:\n"; 
    for (auto& x:sample2) 
        cout << x.first << ":" << x.second << endl; 
  
    // swapping 
    sample1.swap(sample2); 
  
    cout << " Elements of maps after swap \n"; 
    cout << " Elements of first map are:\n"; 
    for (auto& x:sample1) 
        cout << x.first << ":" << x.second << endl; 
  
    cout << " Elements of second map are:\n"; 
    for (auto& x:sample2) 
        cout << x.first << ":" << x.second << endl; 
    return 0; 
}
输出:
Elements of maps before swap 
 Elements of first map are:
d:8
c:6
b:4
a:2
 Elements of second map are:
h:15
f:13
e:11
 Elements of maps after swap 
 Elements of first map are:
h:15
f:13
e:11
 Elements of second map are:
d:8
c:6
b:4
a:2

注意:调用方和被调用方unordered_map都应包含相同类型的元素,否则我们将获得编译时错误。




相关用法


注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 unordered_map swap in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。