I need to map values ranging between lowerBound
and upperBound
to a certain value.
Illustrative Example:
For example, imagine I have GPS system which has users subscribed to it. This system is able to provide me the distance of the user from a certain point. Based on the distance of the user I want to assign them an ID.
Thus users in distance from
1
to100
getID: 8.4
101
to200
getID: 7.2
201
to300
getID: 3.6
401
to600
getID: 4.1
and so on...
My approach:
So what I did, I created an std::map
by initializing it as follows:
std::map<int, double> distanceToIdMap;
distanceToIdMap =
{
{100, 8.4},
{200, 7.2},
{300, 3.6},
};
Then I use this code to get the ID for the given distance:
double roundUpToHundred = std::ceil(realDistance / 100.0) * 100;
double powerForDistance = distanceToIdMap.at(roundUpToHundred);
However my approach breaks down for the 401
to 600
distance, because by ceiling to the nearest hundred for a distance of 400+
I get the value 500
for which I don't have an entry in the map. Of course the trivial solution would be to add an entry for 500
to the distanceToIdMap
but that is not how I want to handle this problem.
I would like to have a map with {(lowerbound, upperbound) , correspondingID}
structure so I can address cases where an ID covers distance spanning more than 100m. And a given can check if the lowerBound
< realDistance
< upperBound
and then provide the ID.
Aucun commentaire:
Enregistrer un commentaire