#include <bits/stdc++.h>
using namespace std;
int main(){
//map的键值对可以是任意类型
//map按照键的大小进行排序(默认升序)
//map与multiset的区别是multiset的键可以重复而map不可以(不管是map还是multiset,他们的键值对的值可以重复)
//map初始化
map<int,string> map1 = {
{4,"four"}
,{0,"zero"}
,{1,"one"}
,{3,"three"}
,{2,"two"}
,{2,"two"}
,{2,"two"}
,{2,"two"}
};
//map键值对遍历输出,同时发现map会默认以键的大小排序,那如果不是int类型呢?
for(auto m:map1)cout<<m.first<<':'<<m.second<<endl;
//插入元素
map1[5] = "five";//相当于插入键值对
map1[1] = "one1";//修改原有的键所对应的值
map1.insert(make_pair(6,"six"));
map1.insert({7,"seven"});
for(auto m:map1)cout<<m.first<<':'<<m.second<<endl;
//删除元素
map1.erase(7);//删除对应键的元素
//map1.clear();//清空
//map按降序排列,如果是字符串,则按照字符ascii码大小比较
map<string,string,greater<string>> map1_reverse = {
{"abc","1"},
{"abd","1"},
{"ab","1"},
{"ac","1"},
{"bc","1"},
{"bb","1"},
};
for(auto m_s:map1_reverse)cout<<m_s.first<<":"<<m_s.second<<endl;
//map容器大小
int size_map = map1_reverse.size();
bool empty_map = map1_reverse.empty();
cout<<size_map<<' '<<empty_map<<endl;
//map查找
map<int,string> map1_find={
{1,"hi1"},
{2,"hi2"},
{3,"hi3"},
{4,"hi4"},
{5,"hi5"},
{6,"hi6"},
{7,"hi7"},
};
auto it = map1_find.find(7);
//end指向map最后一个元素的后一个元素
if(it!=map1_find.end())cout<<it->second<<endl;
else cout<<"查找失败"<<endl;
//multimap与map的区别:multimap的键可重复
multimap<int,string> mul1_map= {
{4,"four"}
,{0,"zero"}
,{1,"one"}
,{3,"three"}
,{2,"two"}
,{2,"two"}
,{2,"two"}
,{2,"two"}
};
for(auto m_m:mul1_map) cout<<m_m.first<<':'<<m_m.second<<endl;
return 0;
}