Question

I need to store some numbers like this

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)
...

I need the fastest random access. Actually it is going to be a table of data for my application. I thought of storing these values inside a header and call them where ever I need. I mostly do scripting (PHP/Python) and there I have Arrays/Dictionaries. But what about C++?

I have found map and unordered_map (which seems the former is better for random access) so far.

Was it helpful?

Solution

You could use a std::unordered_map to hold this, ie: std::unordered_map<int, std::array<int, 4>>.

The use of unordered_map will provide faster access by key. From 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top