fork download
  1. #include <map>
  2. #include <vector>
  3. #include <sstream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. typedef std::map<std::string, std::vector<int> > StringMap;
  12.  
  13. void AddToMap(StringMap& sMap, const std::string& line)
  14. {
  15. istringstream strm(line);
  16. string name;
  17. strm >> name;
  18. StringMap::iterator it = sMap.insert(make_pair(name, std::vector<int>())).first;
  19. int num;
  20. while (strm >> num)
  21. it->second.push_back(num);
  22. }
  23.  
  24. int main()
  25. {
  26. vector<std::string> data = { "Adam 2 5 1 5 3 4", "John 1 4 2 5 22 7", "Kate 7 3 4 2 1 15", "Bill 2222 2 22 11 111" };
  27. StringMap vectMap;
  28.  
  29. // Add results to map
  30. for_each(data.begin(), data.end(), [&](const std::string& s){AddToMap(vectMap, s); });
  31.  
  32. // Output the results
  33. for_each(vectMap.begin(), vectMap.end(),
  34. [](const StringMap::value_type& vt)
  35. {cout << vt.first << " "; copy(vt.second.begin(), vt.second.end(), ostream_iterator<int>(cout, " ")); cout << "\n"; });
  36. }
  37.  
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Adam 2 5 1 5 3 4 
Bill 2222 2 22 11 111 
John 1 4 2 5 22 7 
Kate 7 3 4 2 1 15