fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. multimap<string,string> students;
  11. students.insert(make_pair("a","cs1"));
  12. students.insert(make_pair("b","cs1"));
  13. students.insert(make_pair("b","cs2"));
  14. students.insert(make_pair("a","cs2"));
  15. students.insert(make_pair("c","cs1"));
  16. students.insert(make_pair("a","cs2"));
  17. students.insert(make_pair("c","cs2"));
  18. students.insert(make_pair("c","cs3"));
  19.  
  20.  
  21. std::string current_key = "";
  22. for (auto itr = students.begin(); itr != students.end(); ++itr)
  23. {
  24. if(current_key == itr->first) {
  25. continue;
  26. } else {
  27. current_key = itr->first;
  28. }
  29. const auto result = students.equal_range(itr->first);
  30. cout << itr->first << ": ";
  31. for(auto it = result.first; it != result.second; ++it) {
  32. cout << it->second << " ";
  33. }
  34. cout << endl;
  35.  
  36. }
  37.  
  38. }
Success #stdin #stdout 0s 4452KB
stdin
Standard input is empty
stdout
a: cs1 cs2 cs2 
b: cs1 cs2 
c: cs1 cs2 cs3