Best way to store this data

I'll be using a much smaller example, for simplicity.

I have three parts of data; a character, an integer, and a float.

'A', 2, 4.0
'B', 7, 3.825
'C', 5, 3.125
' ', 9, 3.0

Now I need to use that data based on what a user enters.


So, assuming I'm using three arrays to hold the data, then I would need to search through the array to find 'A', use the data, then for 'B', then for 'C', etc.

My first thought would be an array of size 67 ('C' = 67) so that I could reference like array['C'] to get the data needed. However, this leaves a lot of dead space between 32 and 65 (Space and 'A' ASCII), and there was no third array holding the characters.

So, as a recap, I need to be able to use data based on each character in a string, and that character could (Potentially) Be anything from ASCII 32 - 90, and maybe even higher.

What is the best way to store this data, given that I don't know what characters might be used, and that theoretically, I could need all the way up to ASCII 255?

Hi meesa,

Have a look at std::map - see what you think.

Good Luck!
Perfect. For some reason I was thinking that didn't exist in C++. Don't know why. Note to self: Google even when you're sure.
Btw forgot to mention this : std::map::operator[]

It's handy for when you are not sure if the item already exists, probably easier than using insert.

Be wary of map::operator[]

It will always return you a value, even if there is no pre-existing entry for the key. It will obligingly create a new map entry (with the default value for the type) and return to you. If that's the behaviour you need, fine. But if you need to check if an entry for a given key exists, then you must use map::find.

Similarly, map::insert should be used if you need to be able to detect collisions with existing entries.

Last edited on
Thanks Andy, always appreciate your expert advice :+)
Topic archived. No new replies allowed.