So I have an unordered map, and for each key I want to store two unique floats. These floats represent aggregate values over time of something I am simulating, and thus as my code progresses they values of existing keys may be added to, and new keys may be created.
Previously I was only tracking one value, and so an unordered map was the easy solution. I am not sure how to save two distinct values with one key though?
- Using an
unordered_map<int,vector<float> >was my first thought, but then adding to existing values isn’t as easy. It seems I must determine if a key exists first, and then either add the new vector component-wise to the existing one or set the key equal to the new vector.
- I looked at unordered_multimap. Although I didn’t get a great feel for how it works, it didn’t seem to offer a good way to track which of the values is which, given that I have two values that I want to keep separate and be able to determine which is which.
Is there another way to go about this?
unordered_map<int,pair<float,float> > was an easy solution.
You could use an
unordered_map< int, std::pair< float, float > >, accessing the values via the
.second functions of the pair. I’m not sure that that is “easier” then using the
vector< float > approach though. The vector approach has the advantage of allowing you to expand to store more values with ease. The
pair approach has the advantage of being explicitly two values and only two values.