fork download
  1. #include <string>
  2. #include <unordered_map>
  3. #include <chrono>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iostream>
  7.  
  8. struct Index {
  9. std::vector<std::string> collections;
  10. std::chrono::system_clock::time_point last_query_time;
  11. };
  12.  
  13. struct Database {
  14. std::vector<std::string> collection_names;
  15. std::vector<Index> indexes;
  16. };
  17.  
  18. using time_point = std::chrono::system_clock::time_point;
  19.  
  20. std::unordered_map<std::string, time_point> get_last_query_time(const Database& database) {
  21.  
  22. std::unordered_map<std::string, time_point> last_query;
  23.  
  24. for (auto& c : database.collection_names) {
  25. last_query[c] = {};
  26. }
  27. for (auto& i : database.indexes){
  28. for (auto& c : i.collections) {
  29. last_query[c] = std::max(last_query[c], i.last_query_time);
  30. }
  31. }
  32. return last_query;
  33. }
  34.  
  35. int main() {
  36. Database database{ {"collection1","collection2","collection3"}, {{{"collection1","collection3"},std::chrono::system_clock::now()}} };
  37.  
  38. auto last_query = get_last_query_time(database);
  39.  
  40. for (auto& p : last_query) {
  41. std::cout << p.first << ":" << p.second.time_since_epoch().count() << "\n";
  42. }
  43.  
  44.  
  45. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
collection3:1466478769651581748
collection2:0
collection1:1466478769651581748