English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ 참조 매뉴얼

C++ STL map(컨테이너)

C ++ map map swap() 함수 사용법 및 예제swap()

함수는 두 개의 map의 내용을 교환하는 데 사용되며, 두 개의 map은 같은 타입이어야 하지만 크기는 다를 수 있습니다.

문법

void swap (map& x);

x매개변수

swap() 함수는 내용을 교환하는 map 컨테이너를 사용합니다.

반환 값이 없습니다

实例1

두 개의 map의 요소를 교환하는 간단한 예제를 보겠습니다.

#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은 다음과 같은 요소를 포함합니다" << endl;
   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " << " << it->second << endl;
   return 0;
}

출력:

Map은 다음과 같은 요소를 포함합니다
a = 1
b = 2
c = 3
d = 4
e = 5

위의 예제에서, Map m1다섯 개의 요소를 가지고 있으면 m2이 비어 있을 때, m1교환됩니다.2할 때, m1의 모든 요소가 m으로 교환됩니다.2。

实例2

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

#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 포함: 
";
  for (map<char, int>::iterator it = map1.begin(); it != map1.end(); ++it)
    cout << it->first << " << " << it->second << '\n';
  cout << "map2 포함: 
";
  for (map<char, int>::iterator it = map2.begin(); it != map2.end(); ++it)
    cout << it->first << " << " << it->second << '\n';
  return 0;
}

출력:

map1 포함합니다:
a => 110
b => 220
c => 330
map2 포함합니다:
x => 100
y => 200

위의 예제에서, 두 개의 Map(즉, map1와 map2)的内容相互交换。

实例3

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

#include<iostream>
#include<map>
using namespace std;
 
int main()
{
    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';
 
    //交换map的元素
    swap(map1, map2);
 
    //打印map的元素
    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()函数用于交换两个映射的内容。

实例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));
  // 을 교환한 후1과 m2的内容。
  cout << "交换m1과 m2。\n";
  m1.swap(m2);
  show("Contents of m2: ", m2);
  show("Contents of m1: ", m1);
 // Clear m1.
  m1.clear();
  if(m1.empty()) cout << "m1 为空.";
  return 0;
}
// 使用迭代器显示map<string, int>的内容。
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;
}

출력:

을 교환한 후1과 m2。
m2내용: 
  A, 100
  B, 200
  G, 300
m1내용: 
m1 이 비어 있습니다.

위의 예제에서 Map m1의 내용이 교환되었습니다.2및 Map m1 이후가 지워졌습니다.

C++ STL map(컨테이너)