Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6.  
  7. #include "query.h"
  8. using namespace std;
  9.  
  10.  
  11.  
  12. bool compare(const pair<string,int> &first, const pair<string,int> &second)
  13. {
  14. return (first.second > second.second);
  15. }
  16.  
  17. int main()
  18. {
  19. int RESULTS_TO_PRINT = 0;
  20. bool find;
  21. map<string, int> loginStats;
  22. string line;
  23. while (getline(cin, line))
  24. {
  25.  
  26. Query* q = parseQuery(line.c_str());
  27.  
  28. if (q->action == ACTION_COMMIT)
  29. {
  30. find = false;
  31. for (auto it = loginStats.begin(); it != loginStats.end(); it++) {
  32. if (it->first == q->sender_login) {
  33. it->second++;
  34. find = true;
  35. break;
  36. }
  37. }
  38. if (!find) {
  39. loginStats.insert(pair<string, int>(q->sender_login, 1));
  40. }
  41. delete [] q;
  42. }
  43. if(loginStats.size() >= 3){
  44. RESULTS_TO_PRINT = 3;
  45. }
  46. else {
  47. RESULTS_TO_PRINT = loginStats.size();
  48. }
  49. }
  50.  
  51. vector<pair<string, int> >
  52. statsVec(loginStats.begin(), loginStats.end());
  53.  
  54. sort(statsVec.begin(), statsVec.end(), compare);
  55.  
  56. for (int i = 0; i < RESULTS_TO_PRINT; ++i)
  57. {
  58. cout << statsVec[i].first << " " << statsVec[i].second << "\n";
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement