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


C++ unordered_multimap swap()用法及代碼示例


unordered_multimap::swap()是C++ STL中的內置函數,該函數交換兩個unordered_multimap容器的內容。兩個容器的尺寸可能不同。

用法:

unordered_multimap_name1.swap(unordered_multimap_name2)

參數:該函數接受單個強製性參數unordered_multimap_name2,該參數指定與unordered_multimap_name1進行交換的unordered_multimap。


返回值:它不返回任何東西。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate the 
// unordered_multimap::swap() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample1, sample2; 
  
    // inserts key and element 
    // in sample1 
    sample1.insert({ 10, 100 }); 
    sample1.insert({ 50, 500 }); 
  
    // inserts key and element 
    // in sample1 
    sample2.insert({ 20, 200 }); 
    sample2.insert({ 30, 300 }); 
    sample2.insert({ 30, 150 }); 
  
    cout << "Key and Elements of Sample1 before swap are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\nKey and Elements of Sample2 before swap are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    // performs swap of two 
    // unordered_multimaps 
    sample1.swap(sample2); 
  
    cout << "\n\nKey and Elements of Sample1 after swap are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\nKey and Elements of Sample2 after swap are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    return 0; 
}
輸出:
Key and Elements of Sample1 before swap are:{50, 500} {10, 100} 
Key and Elements of Sample2 before swap are:{20, 200} {30, 150} {30, 300} 

Key and Elements of Sample1 after swap are:{20, 200} {30, 150} {30, 300} 
Key and Elements of Sample2 after swap are:{50, 500} {10, 100}

示例2:

// C++ program to illustrate the 
// unordered_multimap::swap() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample1, sample2; 
  
    // inserts key and element 
    // in sample1 
    sample1.insert({ 'a', 'A' }); 
    sample1.insert({ 'g', 'G' }); 
  
    // inserts key and element 
    // in sample1 
    sample2.insert({ 'b', 'B' }); 
    sample2.insert({ 'c', 'C' }); 
    sample2.insert({ 'd', 'D' }); 
  
    cout << "Key and Elements of Sample1 before swap are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\nKey and Elements of Sample2 before swap are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    // performs swap of two 
    // unordered_multimaps 
    sample1.swap(sample2); 
  
    cout << "\n\nKey and Elements of Sample1 after swap are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\nKey and Elements of Sample2 after swap are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    return 0; 
}
輸出:
Key and Elements of Sample1 before swap are:{g, G} {a, A} 
Key and Elements of Sample2 before swap are:{d, D} {b, B} {c, C} 

Key and Elements of Sample1 after swap are:{d, D} {b, B} {c, C} 
Key and Elements of Sample2 after swap are:{g, G} {a, A}


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap swap() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。