函数原型:
- size(); //返回容器中元素的数目
- empty(); //判断容器是否为空
- swap(st); //交换两个集合容器
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<map>
//大小
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大小为:" << m.size() << endl;
}
}
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
//交换
void test02()
{
map<int, int>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 20));
m1.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(1, 10));
m2.insert(pair<int, int>(2, 20));
m2.insert(pair<int, int>(3, 30));
cout << "交换前:" << endl;
printMap(m1);
printMap(m2);
cout << "交换后:" << endl;
m1.swap(m2);
printMap(m1);
printMap(m2);
}
int main()
{
test01();
system("pause");
return 0;
}