距离地图最近的字符串/时间戳

pyC*_*hon 0 c++

如果时间戳不在存储有时间戳的地图中,我想在地图中找到最接近的匹配时间戳,并使用最接近的值作为键.我有基本的结构设置我正在尝试做什么我只是不知道如何找到最近的时间戳

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
   }    
Run Code Online (Sandbox Code Playgroud)

更新

我试图避免将相当大的代码库转换std::stringuint64_t尽管它会提高性能,但这不是一个大问题,

我无法得到std::map::lower_boundstd::map::upper_bound解决方案在这里工作是我尝试IDE ONE,

http://ideone.com/MnRLIH

Oli*_*rth 5

你也许可以得到你需要什么std::map::lower_bound或者std::map::upper_bound,无论其是为O(log N)的复杂性.


另外,强烈考虑将时间戳存储为,uint64_t而不是字符串.这将大大减少比较和处理的计算量.