C++ STL中unordered_multimap swap()函数

C++ STL中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()
{
  
    // 声明
    unordered_multimap<int, int> sample1, sample2;
  
    // 在sample1中插入键和元素
    sample1.insert({ 10, 100 });
    sample1.insert({ 50, 500 });
  
    // 在sample2中插入键和元素
    sample2.insert({ 20, 200 });
    sample2.insert({ 30, 300 });
    sample2.insert({ 30, 150 });
  
    cout << "交换前Sample1的键和元素为:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n交换前Sample2的键和元素为:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    // 执行两个unordered_multimap的交换
    sample1.swap(sample2);
  
    cout << "\n\n交换后Sample1的键和元素为:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n交换后Sample2的键和元素为:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}
交换前Sample1的键和元素为:{50, 500} {10, 100} 
交换前Sample2的键和元素为:{20, 200} {30, 150} {30, 300} 

交换后Sample1的键和元素为:{20, 200} {30, 150} {30, 300} 
交换后Sample2的键和元素为:{50, 500} {10, 100}

程序2:

// C++程序,说明
// 无序多重映射的swap()函数
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // 声明
    unordered_multimap<char, char> sample1, sample2;
  
    // 将键和元素插入到sample1中
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });
  
    // 将键和元素插入到sample2中
    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 << "} ";
    }
  
    // 执行两个无序多重映射的swap操作
    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}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C++ 教程