fork download
  1. //#include <fstream>
  2. #include <iostream>
  3. #include <map>
  4. #include <sstream>
  5. #include <string>
  6. #include <utility>
  7.  
  8. typedef std::map<std::string,std::string> attribute_map;
  9. typedef std::map<std::string,attribute_map> bird_map;
  10.  
  11. int main()
  12. {
  13. //std::ifstream file("Bird.lst");
  14. std::istream &file = std::cin;
  15.  
  16. bird_map birds;
  17. std::string key;
  18. while(std::getline(file,key))
  19. {
  20. // in case it has windows encoding with end-of-line = \r\n
  21. if (!key.empty() &&
  22. key[key.size()-1] == '\r')
  23. {
  24. key.erase(key.size() - 1);
  25. }
  26.  
  27. if (key.empty())
  28. {
  29. continue;
  30. }
  31.  
  32. attribute_map attributes;
  33. std::string value;
  34. while(std::getline(file,value))
  35. {
  36. // in case it has windows encoding with end-of-line = \r\n
  37. if (!value.empty() &&
  38. value[value.size()-1] == '\r')
  39. {
  40. value.erase(value.size() - 1);
  41. }
  42.  
  43. // if we found the empty string
  44. if(value.empty())
  45. {
  46. break;
  47. }
  48.  
  49. // now split the value into an attribute and a flag
  50. std::string attr_name, attr_value;
  51. std::istringstream ss(value);
  52. ss >> attr_name >> attr_value;
  53. if(attr_name != "vaccinated" && attr_name != "babies" && attr_name != "sale") {
  54. // save the value into the vector
  55. attributes[attr_name] = attr_value;
  56. }
  57. }
  58. // save the bird into the map
  59. birds[key] = attributes;
  60. }
  61.  
  62. // now print the data we collected
  63. for(bird_map::iterator bird = birds.begin();
  64. bird != birds.end();
  65. bird++)
  66. {
  67. std::cout << bird->first << "\n";
  68. for(attribute_map::iterator attribute = bird->second.begin();
  69. attribute != bird->second.end();
  70. attribute++)
  71. {
  72. std::cout << " " << attribute->first
  73. << " " << attribute->second
  74. << "\n";
  75. }
  76. std::cout << "\n";
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.01s 5596KB
stdin
parrot.sh
vaccinated  yes
eat         yes
babies      no
fly         yes
sale        no

pigeon.sh
vaccinated  yes
eat         yes
fly         yes
babies      yes
sale        yes

duck.sh
vaccinated  yes
eat         yes
fly         no
sale        yes
babies      no

flammingo.sh
vaccinated  yes
eat         yes
fly         yes
sale        no
babies      no

eagle.sh
vaccinated  yes
eat         yes
babies      no
fly         yes
stdout
duck.sh
   eat   yes
   fly   no

eagle.sh
   eat   yes
   fly   yes

flammingo.sh
   eat   yes
   fly   yes

parrot.sh
   eat   yes
   fly   yes

pigeon.sh
   eat   yes
   fly   yes