1.概念
map中所有元素都是pair<key,value>,key 是map的键,value 是map的值
所有元素都会根据key自动排序
map/multimap属于关联式容器,底层结构是用二叉树实现。
map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
2.map构造和赋值
map<T1,T2> mp;
map(const map &mp);// 拷贝构造
map &operator=(const map &mp);
#include <iostream>
#include <map>
using namespace std;
void printMap(const map<int,int> &mp)
{
for(map<int,int>::const_iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}cout<<endl;
}
void test(){
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));
printMap(m);
map<int,int>m2(m);// 拷贝构造
printMap(m2);
map<int,int>m3;
m3 = m2;// 赋值
printMap(m3);
}
int main()
{
test();
system("pause");
return 0;
}
3.map大小和交换
#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 test(){
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<<"map为空"<<endl;
}else{
cout<<"map不为空"<<endl;
cout<<"map的大小为:"<<m.size()<<endl;
}
map<int,int>m2(m);
map<int,int>m3;
m3.insert(pair<int,int>(4,40));
m3.insert(pair<int,int>(5,50));
m3.insert(pair<int,int>(6,60));
m3.swap(m2);
printMap(m3);
}
int main()
{
test();
system("pause");
return 0;
}
4.map插入和删除
insert(elem);
clear();
erase(pos);// 删除pos位置的元素,返回删除元素的下一个位置
erase(beg,end);// 删除beg到end之间的元素,左闭右开
erase(key);// 删除key对应的元素
#include <iostream>
#include <map>
using namespace std;
void printMap(const map<int,int> &m){
for(map<int,int>::const_iterator it=m.begin();it!=m.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}cout<<endl;
}
void test(){
map<int,int> m;
m.insert(pair<int,int>(1,10));
m.insert(make_pair(2,20));
m.insert(map<int,int>::value_type(3,30));
m[4]=40;
printMap(m);
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
//清空
m.clear();
m.erase(m.begin(),m.end());
printMap(m);
}
int main()
{
test();
system("pause");
return 0;
}
5. map查找和统计
find(key);// 查找key对应的value,不存在返回end()
count(key);// 统计key出现的次数
#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 test(){
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));
auto pos=m.find(3);// auto自动推导迭代器类型
if(pos!=m.end()){
cout<<"key = "<<pos->first<<" value = "<<pos->second<<endl;
}else{
cout<<"没有找到key = 3"<<endl;
}
int num=m.count(1);
cout<<"key = 1 出现的次数 = "<<num<<endl;
}
int main()
{
test();
system("pause");
return 0;
}
6. map容器排序
map容器默认排序规则为 按照key值进行 从小到大排序
#include <iostream>
#include <map>
using namespace std;
class MyCompare{
public:
bool operator()(int v1,int v2) const{
return v1>v2;
}
};
void test(){
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int,MyCompare>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}