map容器
自身按照key值默认排序
map中所有元素都是成对出现,插入数据时候要使用对组
接口:
判断是否为空——empty()
返回元素个数——size()
交换两个集合容器——swap()
插入——insert() (位置迭代器) 记住一种就可以了
//第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
删除——erase() (位置迭代器)
清空——clear()
查找——find() 例如:map<int,int>:: iterator pos = m.find(3);
统计——count() 对于map容器而言,统计count不是0就是1
#include<iostream>
#include<map>
using namespace std;
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << it->second << endl;
}
cout << endl;
}
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));
m.insert(pair<int, int>(4, 40));
printMap(m);
//拷贝构造
map<int, int>m1(m);
printMap(m1);
//赋值
map<int, int>m2;
m2 = m1;
printMap(m2);
}
int main()
{
test01();
system("pause");
return 0;
}