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

Is the order of items in a hash_map/unordered_map stable?(hash_map/unordered_map 中的项目顺序是否稳定?)
本文介绍了hash_map/unordered_map 中的项目顺序是否稳定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)