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