函数模板 类似 数组有下标一样 可以便利的访问
· 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;
好处:
- 提供一种关联数组
- map 可以用iterators 访问 first second
- Maps are fast O(log(n)) insertion and lookup time