函數(shù)模板 類似 數(shù)組有下標一樣 可以便利的訪問
· std::map<key_type,data_type,[comparison_function]>`
定義一個學生(string)成績(char A≌锘ΑB“ぴ肌C〉澄汀D)的map
std::map <string,char> grade_list精偿;
grade_list["John"]='B';
if you want improve John's grades
grade_list["John"]='A';
So adding keys to a map can be done without doing anything special -- we just need to use key and it will be added automatically along with the corresponding data item.On the other hand,getting rid of an element requires calling the function erase, which is a member of the map class:
erase(key_type key_value);
for instance
grade_list.erase("John");
清空map
·grade_list.clear();·
std::map <string,char> grade_list;
grad_list["John"]='A';
if(grad_list.find("Tim")==grad_list.end())
{
std::cout<<"Tim is not in the map!"<<endl;
}
std::map<string,char> grade_list;
grade_list["John"] ='A';
std::cout<<grade_list.begin()->first<<endl;
std::cout<<grade_list.begin(0->sencond<<endl;
好處:
- 提供一種關(guān)聯(lián)數(shù)組
- map 可以用iterators 訪問 first second
- Maps are fast O(log(n)) insertion and lookup time