Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <map>
  4. #include <sstream>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <unordered_map>
  8.  
  9. using namespace std;
  10.  
  11. void entryFunction(istringstream& istr,unordered_set<string> &setID,
  12. unordered_map<string, int>& nameMap,map<int, int>& ageMap)
  13. {
  14. string ID;
  15. string name;
  16. int age=0;
  17.  
  18. istr>>ID;
  19.  
  20. if(setID.find(ID)==setID.end())
  21. {
  22. setID.insert(ID);
  23. istr>>name>>age;
  24.  
  25. nameMap[name]++;
  26. ageMap[age]++;
  27. }
  28.  
  29. }
  30.  
  31. void nameSearchFunction (istringstream& istr, unordered_map<string, int>& nameMap)
  32. {
  33. string name;
  34. istr>>name;
  35.  
  36. cout<<nameMap[name]<<endl;
  37. }
  38.  
  39. void ageSearchFunction (istringstream& istr,map<int, int>& ageMap)
  40. {
  41. int fromStart=0;
  42. int toEnd=0;
  43.  
  44. istr>>fromStart>>toEnd;
  45. int usersCounter=0;
  46.  
  47. map<int,int>::iterator startIT=ageMap.find(fromStart);
  48. map<int,int>::iterator endIT= ageMap.find(toEnd);
  49.  
  50. for(; startIT!=endIT; ++startIT)
  51. {
  52. usersCounter+=startIT->second;
  53. }
  54. cout<<usersCounter<<endl;
  55. }
  56.  
  57. main()
  58. {
  59. ios_base::sync_with_stdio(0);
  60. cin.tie(NULL);
  61.  
  62. unordered_set<string> setID;
  63. unordered_map<string, int> nameMap;
  64. map<int, int> ageMap;
  65.  
  66. string command;
  67.  
  68.  
  69.  
  70. while(true)
  71. {
  72. ///Take command
  73. getline(cin, command);
  74. istringstream istr (command);
  75. string keyWord;
  76.  
  77. istr>>keyWord;
  78.  
  79. ///Do something
  80. if(keyWord=="entry")
  81. {
  82. entryFunction(istr, setID, nameMap, ageMap);
  83. }else if(keyWord=="name")
  84. {
  85.  
  86. nameSearchFunction (istr, nameMap);
  87. }else if(keyWord=="age")
  88. {
  89.  
  90. ageSearchFunction (istr, ageMap);
  91. }else if(keyWord=="end")
  92. {
  93. break;
  94. }
  95.  
  96.  
  97. }
  98.  
  99.  
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement