C++:为什么我的hash_map会给我一个像地图一样有序的结果
C++: Why my hash_map is giving me an ordered result like a map?
由于map是使用树和hash_map实现的,我创建了一个代码来测试我的map是否会给我一个有序的结果,hash_map会按照注册的顺序打印我的示例
map<string, int> mymap;
hash_map<string, int> myhashmap;
mymap["lucas"] = 1;
mymap["abel"] = 2;
mymap["jose"] = 1;
myhashmap["lucas"] = 1;
myhashmap["abel"] = 2;
myhashmap["jose"] = 1;
for(map<string, int>::iterator it = mymap.begin(); it != mymap.end(); it++){
cout << it->first << " " << it->second << endl;
}
cout << endl;
for(hash_map<string, int>::iterator it = myhashmap.begin(); it != myhashmap.end(); it++){
cout << it->first << " " << it->second << endl;
}
但两个结果都是:
abel 2
jose 1
lucas 1
为什么hashmap给了我一个有序的结果?
hash_map中没有顺序保证,这意味着它可以根据实现情况以任何顺序存储结果。
相关文章:
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何知道地图中的最后一个元素是否被删除?
- 将一个地图的内容附加并替换为另一个地图
- 如何在给定任意数量的整数的情况下创建一个唯一键?并使用该键存储,然后从地图中查找
- 如何在地图迭代器中检测最后一个元素
- 将映射从第二个元素复制到另一个地图
- 带钥匙的C 11地图定义了一个值范围
- 如果地图擦除范围内的第一个和最后一个相等,则是否删除该元素
- 合成地图:从另一个地图获取数据
- 从一个相机到另一个相机的地图点
- 在C 中创建一个巨大的标志地图
- 如何将std ::映射的第一个N元素复制到另一个地图
- 检查最后一个项目是否通过地图密钥存在
- C++:为什么我的hash_map会给我一个像地图一样有序的结果
- 如何得出结论,如果一个单位的士兵在网格地图上连接
- 构建一个地图,它的键可以同时用作SRRing和字符串数组
- 我读了map.erase(map.end());删除地图的最后一个元素。但是最后一个元素是什么?它是否基于元素的插入顺序?
- 访问地图内部的矢量,该矢量位于另一个地图内
- 访问地图的一个"item"