If a timestamp is NOT in a map stored with timestamps I would like to find the closest matching timestamp in the map and use the closest value as a key. I have the basic structure setup for what I’m trying to do i’m just not sure how to find the nearest timestamp
typedef std::map<std::string,int> Map;
Map::iterator it;
Map my_map;
my_map["2010-01-26 17:02:12"]= 1;
my_map["2010-01-25 08:55:29"]= 2;
my_map["2010-01-24 08:55:29"]= 3;
string timestamp = "2010-01-24 08:55:30"; // would return 3
string timestamp1 = "2010-01-27 01:55:30"; // would return 1
it = my_map.find(timestamp);
if(it == my_map.end()){
//not sure how to approach this
}
update
I’m trying to avoid converting a fairly large code base from std::string to uint64_t although it would increase performance, it’s not that big of an issue,
I can’t get the std::map::lower_bound or std::map::upper_bound solutions to work
here is my attempt on IDE ONE,
You can probably get what you need with
std::map::lower_boundorstd::map::upper_bound, either of which is O(log N) complexity.Also, strongly consider storing your timestamps as, say, a
uint64_trather than a string. This would greatly reduce the amount of computation to compare and process.