Definition of map
map<typename1,typename2> mp;
map<int,int> mp;
map<string,int> mp;
map<set<int>,string> mp;
The map needs to determine the type before the mapping (key) and the type after the mapping (value) <>. Fill in two types, the first is the type of the key, the second is the type of the value,
if it is a string to integer mapping , Must use string instead of char array type
Access to the inner elements of the map container Access
through subscripts is the
same as access to ordinary arrays. For example, for a map defined as map<char,int> mp, you can directly use mp['c'] to access its corresponding Integer, the key value in the map is unique
map<char,int> mp;
mp['c']=1;
mp['c']=2;//1被覆盖
cout<<mp['c']<<endl;//2;
Return via iterator
map<typename1,typename2>::iterator it;
map can use it->first to access the key and it->second to access the value
The internal map is implemented by a red-black tree. The
map will be automatically sorted in the order of the key from small to large.
insert() adds a new pair to the map
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp.insert(pair<char,int>('d',4));
map<char,int>::iterator it;
mp.size();
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
//a 1
//b 2
//c 3
//d 4
find()
find(key) returns a map iterator whose key value is key
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it;
it=mp.find('a');
cout<<it->first<<" "<<it->second<<endl; //a 1
Find if an element appears in the map
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it;
it=mp.find('a');
if(it!=mp.end()){
//存在
cout<<"存在"<<endl;
}
else{
cout<<"不存在"<<endl;
}
it=mp.find('d');
if(it!=mp.end()){
//不存在
cout<<"存在"<<endl;
}
else{
cout<<"不存在"<<endl;
}
count (key) returns the number of a key value
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp['c']=4;//会覆盖前面的mp['c']的值
cout<<mp.count('c')<<endl; //1
empty()
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
cout<<mp.empty()<<endl;//0
erase()
erase() has two usages to delete a single element, delete all elements in an interval
mp.erase(it) it is the iterator of the elements to be deleted
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it;
it=mp.find('a');
mp.erase(it);
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" "<<it->second<<endl; //b 2 c 3
}
mp.erase(key) key is the key of the map to be deleted
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it;
mp.erase('b');
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
mp.erase deletes all elements in an interval
mp.erase(first,last) where first is the starting iterator of the interval to be deleted, and last is the next address of the iterator at the end of the interval to be deleted, also That is to delete the left-closed and right-open interval
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp['d']=4;
mp['e']=5;
map<char,int>::iterator it;
it=mp.find('b');
mp.erase(it,mp.end());
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" "<<it->second<<endl; //a 1
}
clear() Clear all elements in the map collection
size() to obtain the logarithm of the map in the map
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp['d']=4;
mp['e']=5;
map<char,int>::iterator it;
mp.size();
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
mp.clear();
cout<<mp.size()<<endl;//0