Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <set>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. class Visitor {
  10.     string id;
  11.     string name;
  12.     unsigned int age;
  13. public:
  14.     string toString() {
  15.         ostringstream s;
  16.         s << this->id << " " << this->name << " " << this->age;
  17.         return s.str();
  18.     }
  19.  
  20.     string getId() const {
  21.         return this->id;
  22.     }
  23.  
  24.     string getName() const {
  25.         return this->name;
  26.     }
  27.  
  28.     unsigned int getAge() const {
  29.         return this->age;
  30.     }
  31.  
  32.     friend istream& operator>>(istream& stream, Visitor& visitor);
  33. };
  34.  
  35. typedef Visitor * VisitorPtr;
  36.  
  37. class IdComp {
  38. public:
  39.     bool operator()(const VisitorPtr & a, const VisitorPtr & b) const {
  40.         return a->getId() < b->getId();
  41.     }
  42. };
  43.  
  44. typedef set<VisitorPtr, IdComp> VisitorSet;
  45.  
  46. istream& operator>>(istream& stream, Visitor& visitor) {
  47.     stream >> visitor.id >> visitor.name >> visitor.age;
  48. }
  49.  
  50. int getInAgeRange(int startAge, int endAge, const map<int, VisitorSet> & byAge) {
  51.     int inRange = 0;
  52.  
  53.     for (map<int, VisitorSet>::const_iterator i = byAge.lower_bound(startAge); i != byAge.lower_bound(endAge); i++) {
  54.         inRange += i->second.size();
  55.     }
  56.  
  57.     return inRange;
  58. }
  59.  
  60. int main() {
  61.     VisitorSet visitors;
  62.  
  63.     map<string, VisitorSet> visitorsByName;
  64.     map<int, VisitorSet> visitorsByAge;
  65.  
  66.     string command;
  67.     while (cin >> command) {
  68.         if (command == "end") {
  69.             break;
  70.         } else if (command == "entry") {
  71.             VisitorPtr visitor = new Visitor();
  72.             cin >> *visitor;
  73.  
  74.             if (visitors.find(visitor) != visitors.end()) {
  75.                 delete visitor;
  76.             } else {
  77.                 visitors.insert(visitor);
  78.                 visitorsByAge[visitor->getAge()].insert(visitor);
  79.                 visitorsByName[visitor->getName()].insert(visitor);
  80.             }
  81.         } else if (command == "name") {
  82.             string name;
  83.             cin >> name;
  84.  
  85.             cout << visitorsByName[name].size() << endl;
  86.         } else if (command == "age") {
  87.             int startAge, endAge;
  88.             cin >> startAge >> endAge;
  89.  
  90.             cout << getInAgeRange(startAge, endAge, visitorsByAge) << endl;
  91.         }
  92.     }
  93.  
  94.     for (VisitorSet::iterator i = visitors.begin(); i != visitors.end(); i++) {
  95.         VisitorPtr vPtr = *i;
  96.         delete vPtr;
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement