fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <string>
  4. #include <vector>
  5. #include <memory>
  6. #include <unordered_map>
  7.  
  8. struct Employee {
  9. std::string FirstName;
  10. std::string LastName;
  11. };
  12.  
  13. typedef std::vector<std::shared_ptr<Employee>> Employees;
  14. typedef std::unordered_map<std::string, Employees> EmployeeByLookup;
  15.  
  16. std::istream& operator>>(std::istream& is, Employee& e) {
  17. return is >> e.FirstName >> e.LastName;
  18. }
  19.  
  20. std::ostream& operator<<(std::ostream& o, const Employee& e) {
  21. return o << e.FirstName << " " << e.LastName;
  22. }
  23.  
  24. std::ostream& operator<<(std::ostream& o, const Employees& employees) {
  25. o << "[";
  26. if (!employees.empty())
  27. {
  28. for(auto e = employees.cbegin(); e != employees.cend()-1; ++e)
  29. o << **e << ", ";
  30. o << *(employees.back());
  31. }
  32. o << "]";
  33. return o;
  34. }
  35.  
  36. EmployeeByLookup CreateDepartmentMap() {
  37. using std::cin;
  38.  
  39. EmployeeByLookup map;
  40. while(cin)
  41. {
  42. std::string department;
  43. cin >> department;
  44. if (department.empty())
  45. break;
  46. auto employee = std::make_shared<Employee>();
  47. cin >> *employee;
  48. map[department].push_back(employee);
  49. }
  50. return map;
  51. }
  52.  
  53. int main() {
  54. using namespace std;
  55.  
  56. EmployeeByLookup departmentMap = CreateDepartmentMap();
  57. cout << "All employees" << endl;
  58. for (auto& x: departmentMap)
  59. cout << "Department: " << x.first << "\n\t" << x.second << std::endl;
  60.  
  61. cout << "--------------" << endl;
  62. string lookFor = "P+0$i";
  63. if (departmentMap.find(lookFor) != departmentMap.end()) {
  64. cout << "Department \"" << lookFor << "\" has "
  65. << departmentMap[lookFor].size() << " employees." << endl
  66. << departmentMap[lookFor] << endl;
  67. }
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 3484KB
stdin
1234 Frank Flinstone
ABCD Bill Williams
1234 Leisure Larry
P+0$i Tom Tommson
P+0$i John Doe
stdout
All employees
Department: P+0$i
	[Tom Tommson, John Doe]
Department: ABCD
	[Bill Williams]
Department: 1234
	[Frank Flinstone, Leisure Larry]
--------------
Department "P+0$i" has 2 employees.
[Tom Tommson, John Doe]