C++ std map

函数模板 类似 数组有下标一样 可以便利的访问

· std::map`

定义一个学生(string)成绩(char A B C D)的map
std::map 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  grade_list;
grad_list["John"]='A';
if(grad_list.find("Tim")==grad_list.end())
{
  std::cout<<"Tim is not in the map!"<
std::map grade_list;
grade_list["John"] ='A';
std::cout<first<sencond<

好处:

  • 提供一种关联数组
  • map 可以用iterators 访问 first second
  • Maps are fast O(log(n)) insertion and lookup time

你可能感兴趣的:(C++ std map)