fork(1) download
  1. #include <vector>
  2. #include <map>
  3. #include <string>
  4. #include <iostream>
  5. #include <stdexcept>
  6. #include <sstream>
  7. int main()
  8. {
  9. std::multimap<std::string, std::string> m;
  10. std::string s = "1-1-2-1,1-1-3-1,1-2-3-4,2-3-4-5,2-3-5-8";
  11. std::stringstream ss(s);
  12. for(std::string item; std::getline(ss, item, ','); )
  13. {
  14. std::stringstream ss2(item);
  15. std::vector<std::string> tokens;
  16. for(std::string tok; std::getline(ss2, tok, '-'); )
  17. tokens.push_back(tok);
  18. if(tokens.size() != 4)
  19. throw std::runtime_error("parsing error at item " + item);
  20. std::string key = tokens[0] + '-' + tokens[1];
  21. std::string value = tokens[2] + '-' + tokens[3];
  22. m.insert(std::make_pair(key, value));
  23. }
  24. std::cout << "Key\tValue\n";
  25. for(auto i = m.begin(); i!=m.end(); ++i)
  26. std::cout << i->first << "\t" << i->second << '\n';
  27. }
  28.  
Success #stdin #stdout 0s 2972KB
stdin
Standard input is empty
stdout
Key	Value
1-1	2-1
1-1	3-1
1-2	3-4
2-3	4-5
2-3	5-8