对std :: map <string,double>进行排序

pyC*_*hon 0 c++ c++11

我有一张时间戳和一些数据的地图我 map<string,double> mymap; 怎样才能按时间戳对它进行排序,以便所有内容都按照chrnological顺序排列?下面是一个里面的样本数据集,我有什么.

 < timestamp           ,         data>

"2011-02-04 14:14:51"             1
"2010-09-24 07:45:13"             2
"2011-10-28 11:10:32"             3
"2008-10-27 11:10:32"             4
"2008-10-27 11:10:33"             5
Run Code Online (Sandbox Code Playgroud)

期望的结果

"2008-10-27 11:10:32"             4
"2008-10-27 11:10:33"             5
"2010-09-24 07:45:13"             2
"2011-02-04 14:14:51"             1
"2011-10-28 11:10:32"             3
Run Code Online (Sandbox Code Playgroud)

Ben*_*enj 8

你真的很幸运,因为你的时间戳是YMD H:M:S(ISO 8601),时间排序顺序与字符串排序顺序相同.因为std::string有一个operator<(),std::map如果你遍历它,你的意志已经在日期顺序.你所要做的就是填写地图,它会自行订购.

当然,以数字或日期对象格式存储日期确实是个好主意boost::posix_time::ptime,例如.无论是在内存还是处理方面,这肯定会更高效,因为昂贵的字符串比较将被廉价的整数比较所取代.