22_kvdb.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. map<string, string> db;
  8. cout << "Welcome to the simplest key-value database" << endl;
  9. while (1)
  10. {
  11. cout << "What do you want to do?" << endl;
  12. cout << "Enter P to [P]ut, G to [G]et or L to [L]ist" << endl;
  13. cout << "Or enter Q to [Q]uit" << endl;
  14. string action;
  15. getline(cin, action);
  16. if (action == "P")
  17. {
  18. string key;
  19. string data;
  20. cout << "Enter key: ";
  21. getline(cin, key);
  22. cout << "Enter data: ";
  23. getline(cin, data);
  24. db[key] = data;
  25. }
  26. if (action == "G")
  27. {
  28. cout << "Enter key: ";
  29. string key;
  30. getline(cin, key);
  31. if (db.find(key) != db.end())
  32. {
  33. cout << "Your data: " << db[key] << endl;
  34. }
  35. else
  36. cout << "No such key" << endl;
  37. }
  38. if (action == "L")
  39. {
  40. cout << "DB contents:" << endl;
  41. for (auto it = db.begin(); it != db.end(); it++)
  42. {
  43. cout << it->first << ": " << it->second << endl;
  44. }
  45. }
  46. if (action == "Q")
  47. {
  48. cout << "Bye" << endl;
  49. break;
  50. }
  51. cout << endl;
  52. }
  53. return 0;
  54. }