fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.  
  10. typedef std::map<std::string,int> mapT;
  11.  
  12. mapT my_map;
  13. my_map["2010-01-26 17:02:12"]= 1;
  14. my_map["2010-01-25 08:55:29"]= 2;
  15. my_map["2010-01-24 08:55:29"]= 3;
  16. my_map["2010-01-23 08:55:29"]= 4;
  17.  
  18. string timestamp = "2010-01-24 08:55:30"; // should return 3
  19.  
  20.  
  21. cout<<"using upper bound"<<endl;
  22. mapT::iterator it= my_map.find(timestamp);
  23. if(it == my_map.end()) {
  24. mapT::iterator itup = my_map.upper_bound(timestamp);
  25. cout<<itup->second<<endl;
  26.  
  27. }
  28. cout<<"using lower bound"<<endl;
  29. it= my_map.find(timestamp);
  30. if(it == my_map.end()) {
  31. mapT::iterator itlow = my_map.lower_bound(timestamp);
  32. cout<<itlow->second<<endl;
  33. }
  34.  
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3068KB
stdin
Standard input is empty
stdout
using upper bound
2
using lower bound
2