fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5. #include <iterator>
  6.  
  7. typedef std::vector< std::pair<int, std::string>> Categories;
  8.  
  9. int main()
  10. {
  11. std::map<std::string, Categories> cats;
  12.  
  13. cats["key1"] = {std::make_pair(1, "pair1"), std::make_pair(1, "pair2"), std::make_pair(1, "par3")};
  14. cats["key2"] = {std::make_pair(2, "pair1"), std::make_pair(2, "pair2")};
  15. cats["key3"] = {std::make_pair(3, "pair1")};
  16.  
  17. std::cout << "Range based loop \n";
  18. for(const auto& it: cats)
  19. {
  20. std::cout << it.first << " = "; // keys
  21. for(const auto& ve: it.second) // values vec
  22. std::cout << ve.first << "-" << ve.second << "\t";
  23. std::cout << std::endl;
  24. }
  25.  
  26. std::cout << "\nIterator loop \n";
  27. std::map<std::string, Categories>::const_iterator it;
  28. std::vector< std::pair<int, std::string>>::const_iterator curr_val_it;
  29. for(it = cats.cbegin(); it != cats.cend(); ++it)
  30. {
  31. std::cout << it->first << " = "; // keys
  32.  
  33. for(curr_val_it = it->second.cbegin(); curr_val_it != it->second.cend(); ++curr_val_it )
  34. std::cout << curr_val_it->first << "-" << curr_val_it->second << "\t"; // values vec
  35.  
  36. std::cout << std::endl;
  37. }
  38.  
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Range based loop 
key1 = 1-pair1	1-pair2	1-par3	
key2 = 2-pair1	2-pair2	
key3 = 3-pair1	

Iterator loop 
key1 = 1-pair1	1-pair2	1-par3	
key2 = 2-pair1	2-pair2	
key3 = 3-pair1