unordered_map使用技巧

#include <unordered_map>

//unordered_map存储内不是线性的,因此使用unordered_map时,相对顺序会改变,但事实KEY-VALUE是不变的,而且也保证了KEY值唯一性
//map是按照key字典序进行存储的,但是unordered_map中是无序的
//map内部实现了一个红黑树(非严格平衡二叉树),该结构具有自动排序的功能,因此该map内部的所有元素都是有序的。
//unordered_map内部是一个哈希表,哈希表不会根据key值大小进行排序,存储时是根据key的hash值判断元素是否相同,因此unordered_map内部元素是无序的。

unordered_map<string, int> mm;
mm["AAA"] = 1;
mm["BBB"] = 2;
unordered_map<string, int>::iterator it = mm.begin();
cout << it->first << " " << it->second << endl;	//AAA 1
mm["c"] = 11;
mm["d"] = 1;
it = mm.begin();
cout << it->first << " " << it->second << endl;	//c 11
发布了12 篇原创文章 · 获赞 9 · 访问量 347

猜你喜欢

转载自blog.csdn.net/weixin_43547522/article/details/104695417