📜  C++ STL中的unordered_multimap swap()函数

📅  最后修改于: 2021-05-30 14:03:00             🧑  作者: Mango

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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap 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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap 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}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”