當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。