c++ - 在 C++ 中存储一些数字的方法(有关详细信息,请参见内部)

标签 c++ list dictionary data-structures unordered-map

我需要像这样存储一些数字

key => (four integers, between 0 to 30 (maxmimum), -1 means NULL)
125 => (1,3,5,20)
80 => (4,2,-1,-1)
20 => (10,12,21,3)
...

我需要最快的随机访问。实际上,它将成为我的应用程序的数据表。我想将这些值存储在 header 中,并在需要的地方调用它们。我主要编写脚本 (PHP/Python),并且我有数组/字典。但是 C++ 呢?

到目前为止,我已经找到了 map 和 unordered_map(似乎前者更适合随机访问)。

最佳答案

你可以使用 std::unordered_map 持有这个,即:std::unordered_map<int, std::array<int, 4>> .

unordered_map的使用将通过 key 提供更快的访问。来自 this documentation :

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

关于c++ - 在 C++ 中存储一些数字的方法(有关详细信息,请参见内部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19665049/

相关文章:

c++ - 从 C++ STL 集合中获取通用 key_type

python - 如何创建一个字符串列表,其中只有最后一个元素不同

java - 处理流 java 8 中的异常

python - 在 Python 3 中以恒定时间从字典中选择随机值?

c++ - 显示指针结构中的元素

c++ - 在 opencv 中创建 4x4 mat 矩阵时出错

c++ - 重叠计算

c++如何防止函数参数评估

python - 列表理解结合 2 个列表

python - 创建以动态数量的值作为参数的字典