stl库
map
#include<iostream>
#include<map>
using namespace std;
map<char,int> map2;
struct cmp
{
bool operator()(const char& lhs,const char& rhs)
{
return lhs<rhs;
}
};
bool fncomp(char lhs,char rhs)
{
return lhs<rhs;
}
int main()
{
map2['a']=10;
map2['b']=60;
map2['c']=50;
map2['d']=40;
map2['e']=30;
map<char,int> map1(map2);
//or
// map<char,int> map1(map2.begin(),map2.end());
map<char,int,cmp> map4;
//bool(*fn_pt)=(char,char)fncomp;
//map<char,int,bool(*)(char,char)>map5(ft_nt);
map1.insert({'g',400}); //waring
map1.insert(make_pair('f',800));
// map1.insert(map<int,string>::value_type(2,"fff")); //error
//map1.insert(map<int,char>::value_type('m',2));
map<char,int>::iterator it;
for(it=map1.begin(); it!=map1.end(); it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
cout<<"g=> "<<map1.find('g')->second<<endl;
cout<<"a=> "<<map1.find('a')->second<<endl;
// cout<<"i=> "<<map1.find('i')->second<<endl;
cout<<"m=> "<<map1.find('m')->second<<endl;
cout<<map1['a']<<endl;
cout<<map1['q']<<endl; //0
map1.clear();
map1['a']=20;
map1['b']=62;
while(!map1.empty())
{
cout<<map1.begin()->first<<"=>";
cout<<map1.begin()->second<<endl;
map1.erase(map1.begin());
}
map1['c']=68;
map1.erase('c');
// cout<<map1.begin()->first<<"=>";
// cout<<map1.begin()->second<<endl;
map1['a']=25;
map1['b']=67;
map<char,int>::key_compare key_comp;
key_comp = map1.key_comp();
char highset = map1.rbegin()->first;
it = map1.begin();
do
{
cout<<(*it).first<<"=>"<<(*it).second<<endl;
}
while(key_comp((*it++).first,highset));
cout<<map1.size()<<endl;
return 0;
}