fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. // simulate the file:
  7. std::istringstream in(
  8. R"(Driver1 3
  9. Donkey11 4 500 800 1200 450
  10. Donkey12 3 800 700 600
  11. Donkey13 5 120 150 200 500 300
  12. Driver2 2
  13. Donkey21 3 910 220 410
  14. Donkey22 6 130 150 170 120 160 140
  15. Driver3 5
  16. Donkey31 5 120 210 230 330 400
  17. Donkey32 4 800 700 600 500
  18. Donkey33 2 1000 800
  19. Donkey34 1 1750
  20. Donkey35 3 500 1000 400
  21. )"
  22. );
  23.  
  24.  
  25. int main()
  26. {
  27. using weight_container = std::vector<unsigned>;
  28. using donkey_type = std::pair<std::string, weight_container>;
  29. using donkey_container = std::vector<donkey_type>;
  30.  
  31. std::map<std::string, donkey_container> driver_map;
  32.  
  33. std::string driver_name;
  34. while (in >> driver_name)
  35. {
  36. donkey_container donkeys ;
  37.  
  38. unsigned num_donkeys;
  39. in >> num_donkeys;
  40.  
  41. for (unsigned i = 0; i < num_donkeys; ++i)
  42. {
  43. donkey_type donkey;
  44. in >> donkey.first;
  45.  
  46. unsigned num_boxes;
  47. in >> num_boxes;
  48. donkey.second.resize(num_boxes);
  49.  
  50. for (unsigned j = 0; j < num_boxes; ++j)
  51. in >> donkey.second[j];
  52.  
  53. donkeys.emplace_back(std::move(donkey));
  54. }
  55.  
  56. driver_map.emplace(driver_name, donkeys);
  57. }
  58.  
  59. for (auto& item : driver_map)
  60. {
  61. auto& donkeys = item.second;
  62.  
  63. std::cout << item.first << " has " << donkeys.size() << " donkeys.\n";
  64. for (auto& donkey : donkeys)
  65. {
  66. auto& name = donkey.first;
  67. auto& weights = donkey.second;
  68.  
  69. std::cout << '\t' << name << " has " << weights.size() << " boxes weighing ";
  70. for (auto weight : weights)
  71. std::cout << weight << ' ';
  72. std::cout << '\n';
  73. }
  74. }
  75. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Driver1 has 3 donkeys.
	Donkey11 has 4 boxes weighing 500 800 1200 450 
	Donkey12 has 3 boxes weighing 800 700 600 
	Donkey13 has 5 boxes weighing 120 150 200 500 300 
Driver2 has 2 donkeys.
	Donkey21 has 3 boxes weighing 910 220 410 
	Donkey22 has 6 boxes weighing 130 150 170 120 160 140 
Driver3 has 5 donkeys.
	Donkey31 has 5 boxes weighing 120 210 230 330 400 
	Donkey32 has 4 boxes weighing 800 700 600 500 
	Donkey33 has 2 boxes weighing 1000 800 
	Donkey34 has 1 boxes weighing 1750 
	Donkey35 has 3 boxes weighing 500 1000 400