C++ std map

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

· 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容