fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. int main() {
  6. // type helpers
  7. using MyVec = std::vector<int>;
  8. using MyMap = std::map<unsigned int, MyVec>;
  9.  
  10. // create v1
  11. MyVec v1 { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
  12. // Or MyVec v1; v1.resize(10);
  13.  
  14. // create the map
  15. MyMap x;
  16.  
  17. unsigned int key = 123;
  18. auto it = x.find(key);
  19. if (it == x.end())
  20. x[key] = v1; // causes a COPY of the entire vector
  21.  
  22. for (const auto& idx: x) {
  23. // idx.first is the key
  24. // idx.second is the vector
  25. std::cout << idx.first << ": ";
  26. for (auto val: idx.second) {
  27. // ^ no &, makes copies of values but they're ints so it's ok.
  28. std::cout << val << " ";
  29. }
  30. std::cout << "\n";
  31. }
  32. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
123: 10 20 30 40 50 60 70 80 90 100