std::unordred_map 不保留插入顺序吗?
Doesn't std::unordred_map preserve insertion order?
我对 unordered_map
的理解是每个键存储单位值而不对它们进行排序。但是是否期望不保留插入顺序?
当我编译时 运行:
std::unordered_map<std::string,int> temp;
temp["Start"] = 0;
temp["Read"] = 0;
for ( auto iter : temp )
{
std::cout << iter.first.c_str();
}
使用 VS2015,输出
Start
Read
使用 GCC 4.9 for Android,输出:
Read
Start
这是错误还是预期?
这是意料之中的。在标准中,不保证 std::unordered_map
.
中元素的顺序
来自here:
Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).
我认为总结得差不多了。
我对 unordered_map
的理解是每个键存储单位值而不对它们进行排序。但是是否期望不保留插入顺序?
当我编译时 运行:
std::unordered_map<std::string,int> temp;
temp["Start"] = 0;
temp["Read"] = 0;
for ( auto iter : temp )
{
std::cout << iter.first.c_str();
}
使用 VS2015,输出
Start
Read
使用 GCC 4.9 for Android,输出:
Read
Start
这是错误还是预期?
这是意料之中的。在标准中,不保证 std::unordered_map
.
来自here:
Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).
我认为总结得差不多了。