gpt4 book ai didi

c++ - hash_map/unordered_map 中的项目顺序是否稳定?

转载 作者:行者123 更新时间:2023-11-30 02:02:03 25 4
gpt4 key购买 nike

是否保证当 hash_map/unordered_map 加载相同的项目时,它们在迭代时具有相同的顺序?基本上我有一个 HashMap ,我从一个文件加载它,我定期将有限数量的项目提供给一个例程,之后我释放 HashMap 。项目被消费后,我将相同的文件重新加载到 hashmap 并希望在我上次停止的点之后获取下一批项目。我停止的点将由键标识。

最佳答案

技术上不,不能保证它们以任何特定顺序排列。

但是在实践中,假设您使用确定性哈希函数,您想要做的应该没问题。

考虑

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;
}

您可以合理地预期 map1map2 中的名称-值对以相同的顺序排列。

关于c++ - hash_map/unordered_map 中的项目顺序是否稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com