C++ Map 中的 swap函数

首页 / C++入门教程 / C++ Map 中的 swap函数

C++映射 swap()函数用于交换(或交换)两个映射的内容,但是两个映射必须是同一类型,尽管大小可能有所不同。

swap - 语法

void swap (map& x);

swap - 参数

x :与之交换内容的映射集合。

swap - 例子1

让我们看一个简单的示例,将一个映射的元素交换到另一个。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m1 = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'e', 5},
      };

   map<char, int> m2;

   m2.swap(m1);

   cout << "Map contains following elements" << endl;

   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

输出:

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5

在上面的示例中,映射m1具有五个元素,而m2为空。当您将m1交换为m2时,m1的所有元素都将交换为m2。

swap - 例子2

让我们看一个简单的示例,交换两个映射的内容。

无涯教程网

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,int> map1,map2;

  map1['x']=100;
  map1['y']=200;

  map2['a']=110;
  map2['b']=220;
  map2['c']=330;

  map1.swap(map2);

  cout << "map1 contains:\n";
  for (map<char,int>::iterator it=map1.begin(); it!=map1.end(); ++it)
    cout << it->first << " => " << it->second << '\n';

  cout << "map2 contains:\n";
  for (map<char,int>::iterator it=map2.begin(); it!=map2.end(); ++it)
    cout << it->first << " => " << it->second << '\n';

  return 0;
}

输出:

map1 contains:
a => 110
b => 220
c => 330

map2 contains:
x => 100
y => 200

在上面的例子中,两个映射即map1和map2的内容相互交换。

swap - 例子3

让我们看一个简单的示例,交换两个映射的内容。

无涯教程网

#include<iostream>
#include<map>
using namespace std;
 
int main()
{
   //Take any two maps
    map<int, char> map1, map2;
 
    map1[1] = 'a';
    map1[2] = 'b';
    map1[3] = 'c';
    map1[4] = 'd';  
 
    map2[5] = 'w';
    map2[6] = 'x';
    map2[7] = 'y';
 
   //Swap elements of maps
    swap(map1, map2);
 
   //Print the elements of maps
    cout << "map1:\n"<< "\tKEY\tELEMENT\n";
    for (auto it = map1.begin();
         it != map1.end(); it++)
 
        cout << "\t" << it->first << "\t" << it->second << '\n';
 
    cout << "map2:\n"<< "\tKEY\tELEMENT\n";
    for (auto it = map2.begin();
         it != map2.end(); it++)
 
        cout << "\t" << it->first << "\t" << it->second << '\n';
 
    return 0;
}

输出:

map1:
	KEY	ELEMENT
	5	w
	6	x
	7	y
map2:
	KEY	ELEMENT
	1	a
	2	b
	3	c
	4	d

在上面的示例中,另一种形式的swap()函数用于交换两个映射的内容。

swap - 例子4

让我们看一个简单的例子。

#include <iostream>
#include <string>
#include <map>

using namespace std;

void show(const char *msg, map<string, int> mp);

int main() {
  map<string, int> m1, m2;

  m1.insert(pair<string, int>("A", 100));
  m1.insert(pair<string, int>("G", 300));
  m1.insert(pair<string, int>("B", 200));

 //Exchange the contents of m1 and m2.
  cout << "Exchange m1 and m2.\n";
  m1.swap(m2);
  show("Contents of m2: ", m2);
  show("Contents of m1: ", m1);

//Clear m1.
  m1.clear();
  if(m1.empty()) cout << "m1 is now empty.";

  return 0;
}

// Display the contents of a map<string, int> by using an iterator.
void show(const char *msg, map<string, int> mp) {
  map<string, int>::iterator itr;

  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << itr->first << ", " << itr->second << endl;
  cout << endl;
}

输出:

Exchange m1 and m2.
Contents of m2: 
  A, 100
  B, 200
  G, 300

Contents of m1: 
m1 is now empty.

在上面的示例中,映射m1的内容被交换到映射m2,并且在交换m1映射后已被清除。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学大数据 -〔李智慧〕

10x程序员工作法 -〔郑晔〕

面试现场 -〔白海飞〕

Linux实战技能100讲 -〔尹会生〕

编译原理之美 -〔宫文学〕

A/B测试从0到1 -〔张博伟〕

数据分析思维课 -〔郭炜〕

玩转Vue 3全家桶 -〔大圣〕

手把手教你落地DDD -〔钟敬〕

好记忆不如烂笔头。留下您的足迹吧 :)