gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:12:52 24 4
gpt4 key购买 nike

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

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/

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