fork(3) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <string>
  5. #include <unordered_map>
  6.  
  7. struct Person
  8. {
  9. std::string code;
  10. int data;
  11. };
  12.  
  13. typedef std::unordered_map<int, Person> PersonMap;
  14.  
  15. int main()
  16. {
  17. //std::ifstream fin("test.txt"); // here std::cin
  18.  
  19. PersonMap persons;
  20.  
  21. /* line by line reading */
  22. for (std::string line; std::getline(std::cin, line); )
  23. {
  24. std::istringstream iss(line);
  25.  
  26. int personId;
  27. Person personData;
  28.  
  29. /* parse line as std::string, int, int */
  30. iss >> personData.code >> personId >> personData.data;
  31.  
  32. /* insert into map and save result */
  33. std::pair<PersonMap::iterator, bool> insertResult =
  34. persons.insert(std::pair<int, Person>(personId, personData));
  35.  
  36. /* if personId is already there */
  37. if (!insertResult.second)
  38. {
  39. insertResult.first->second.data += personData.data;
  40. }
  41. }
  42.  
  43. /* output whole map */
  44. for(auto const &person : persons)
  45. {
  46. std::cout << person.first << " : " << person.second.data << "\n";
  47. }
  48. std::cout << std::flush;
  49. }
Success #stdin #stdout 0s 16072KB
stdin
A 123456 0
G 123456 5
A 235334 0
B 123456 2
stdout
235334 : 0
123456 : 7