fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. struct person
  10. {
  11. string name;
  12. string first_name;
  13. unsigned age;
  14. char gender;
  15. };
  16.  
  17. bool operator<(const person& lhs, const person& rhs)
  18. {
  19. if(lhs.name < rhs.name) return true;
  20. return lhs.first_name < rhs.first_name;
  21. }
  22.  
  23. ostream& operator<<(ostream& os, const person& dude)
  24. {
  25. return os << dude.first_name << " " << dude.name << " " << dude.age << " " << dude.gender;
  26. }
  27.  
  28. struct registry
  29. {
  30. vector<person> people;
  31.  
  32. void print(string first_name)
  33. {
  34. for(vector<person>::iterator iter = people.begin(); iter != people.end(); ++iter)
  35. if(iter->first_name == first_name)
  36. cout << *iter << endl;
  37. }
  38.  
  39. void print_all()
  40. {
  41. for(vector<person>::iterator iter = people.begin(); iter != people.end(); ++iter)
  42. cout << *iter << endl;
  43. }
  44.  
  45. void remove(string first_name, string name)
  46. {
  47. cout << "remove " << first_name << " " << name << endl;
  48. for(vector<person>::iterator iter = people.begin(); iter != people.end(); ++iter)
  49. if(iter->first_name == first_name && iter->name == name)
  50. {
  51. people.erase(iter);
  52. return;
  53. }
  54. }
  55.  
  56. void remove_all()
  57. {
  58. people.resize(0);
  59. }
  60.  
  61. void sort()
  62. {
  63. std::sort(people.begin(), people.end());
  64. }
  65. };
  66.  
  67. void cmd_add(registry& reg, istream& in)
  68. {
  69. cout << "cmd_add\n";
  70.  
  71. person new_person;
  72. in >> new_person.first_name;
  73. in >> new_person.name;
  74. in >> new_person.gender;
  75. in >> new_person.age;
  76.  
  77. reg.people.push_back(new_person);
  78. }
  79.  
  80. void cmd_print(registry& reg, istream& in)
  81. {
  82. string filter;
  83. in >> filter;
  84. if(filter == "all")
  85. reg.print_all();
  86. else
  87. reg.print(filter.c_str() + 10);
  88. }
  89.  
  90. void cmd_delete(registry& reg, istream& in)
  91. {
  92. string filter;
  93. in >> filter;
  94. if(filter == "all")
  95. reg.remove_all();
  96. else
  97. {
  98. string second_filter;
  99. in >> second_filter;
  100. reg.remove(filter.c_str() + 10, second_filter.c_str() + 5);
  101. }
  102. }
  103.  
  104. void cmd_sort(registry& reg, istream& in)
  105. {
  106. reg.sort();
  107. }
  108.  
  109. int main()
  110. {
  111. std::map<string, void (*)(registry&, istream&)> commands;
  112. commands["add"] = cmd_add;
  113. commands["print"] = cmd_print;
  114. commands["delete"] = cmd_delete;
  115. commands["sort"] = cmd_sort;
  116.  
  117. registry reg;
  118.  
  119. while(cin.good())
  120. {
  121. string command;
  122. cin >> command;
  123. commands[command](reg, cin);
  124. }
  125. }
Success #stdin #stdout 0.01s 2880KB
stdin
add john doe M 35
add mary croskery F 21
add laurie lamour F 20
print all
print laurie
delete john doe
print mary
print all
sort
print all
stdout
cmd_add
cmd_add
cmd_add
john doe 35 M
mary croskery 21 F
laurie lamour 20 F
remove  
john doe 35 M
mary croskery 21 F
laurie lamour 20 F
laurie lamour 20 F
mary croskery 21 F
john doe 35 M