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

为什么 hash_map 给我一个有序的结果?

hash_map 中没有顺序保证 - 这意味着它可以 任意 顺序存储结果,具体取决于实现。