在hash_map / unordered_map中的项目顺序是否稳定? [英] Is the order of items in a hash_map/unordered_map stable?

查看:179
本文介绍了在hash_map / unordered_map中的项目顺序是否稳定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它保证,当一个hash_map / unordered_map加载相同的项目,它们将有相同的顺序时迭代?基本上我有一个hashmap,我从一个文件加载,并从其中定期喂一个有限数量的项目到一个例程,之后我释放hashmap。在项目消耗后,我重新加载相同的文件到hashmap,并希望获得下一批项目,在我停止上一次的点后。

Is it guaranteed that when a hash_map/unordered_map is loaded with the same items, they will have the same order when iterated? Basically I have a hashmap which I load from a file, and from which I periodically feed a limited number of items to a routine, after which I free the hashmap. After the items are consumed, I re-load the same file to the hashmap and want to get the next batch of items after the point where I stopped the previous time. The point at which I stop would be identified by the key.

推荐答案

技术上没有,它们不能保证在任何特定的

Technically no, they are not guaranteed to be in any particular order.

然而,在实践中,由于你使用确定性哈希函数,你想做什么应该是罚款。

In practice however, given that you use deterministic hash function, what you want to do should be fine.

考虑

std::string name;
std::string value;

std::unordered_map <std::string, std::string> map1;
std::unordered_map <std::string, std::string> map2;

while (read_pair (name, value))
{
    map1[name] = value;
    map2[name] = value;
}

你可以合理地期望 map1 map2 按相同的顺序。

you can reasonably expect that name-value pairs in map1 and map2 go in the same order.

这篇关于在hash_map / unordered_map中的项目顺序是否稳定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆