unordered_map的原理和使用

来源:互联网 发布:ubuntu怎么查看分区 编辑:程序博客网 时间:2024/06/19 02:40

1.原理
unordered_map: unordered_map内部实现了一个哈希表,因此其元素的排列顺序是杂乱的,无序的
2.使用
a.查找元素是否存在unordered_map<int, int> map中是否存在x:
map.find(x)!=map.end()或
map.count(x)!=0
b.插入数据
map.insert(Map::value_type(1,”Raoul”));
c.遍历
unordered_map<key,T>::iterator it;
(*it).first; //the key value
(*it).second //the mapped value
for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
{ cout<<”key value is”<first;
cout<<” the mapped value is “<< iter->second;}
3.评价
优点:
因为内部实现了哈希表,因此其查找速度非常的快
缺点:
哈希表的建立比较耗费时间
适用处:
对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map

0 0
原创粉丝点击