fork download
  1. // CPP program to demonstrate working of map
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. map<int, int> m;
  8.  
  9. m[1] = 2; // Insertion by indexing
  10. m[2] = 3;
  11. m[2] = 3;
  12. // Direct pair insertion
  13. m.insert({ 4, 5 });
  14. m.insert({ 4, 7 });
  15.  
  16. // Insertion of pair by make_pair
  17. m.insert(make_pair(8, 5));
  18.  
  19. cout << "Elements in map:\n";
  20. for (auto it : m)
  21. cout << "[ " << it.first << ", "
  22. << it.second << "]\n"; // Sorted
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 5400KB
stdin
Standard input is empty
stdout
Elements in map:
[ 1, 2]
[ 2, 3]
[ 4, 5]
[ 8, 5]