fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void pMap(map<string,int> m){
  5. cout << "size = " << m.size() << endl;
  6. for(auto it = m.begin();it != m.end();++it){
  7. cout << it->first << " " << it->second << endl;
  8. //cout << (*it).first << " " << (*it).second << endl;
  9. // string s = it->first;
  10. // cout << s << " " << m[s] << endl;
  11. }
  12. for(const auto &pair : m){
  13. cout << pair.first << " " << pair.second << endl;
  14. }
  15. cout << endl;
  16. }
  17.  
  18. int main(){
  19. map<string,int> m;
  20.  
  21. //basic initialization
  22. // m["date"] = 22;
  23. // m["month"]; //declared and initialized to zero.
  24. // m["month"] = 05, m["year"] = 2004;
  25. // pMap(m);
  26.  
  27. //initializing using pair.
  28. // m.insert({"Date",22});
  29. // m.insert(make_pair("month",05));
  30. // m["year"];
  31. // m.insert({"year",2004});
  32. // pMap(m);
  33. // //insert() doesnt update pre-existent keys!!!
  34. // m["year"] = 2004;
  35. // pMap(m);
  36.  
  37. //using find(key):return an it for the key.
  38. // map<int,int> m2 = {{1,6},{2,8},{4,9}};
  39. // int n; cin >> n;
  40. // auto it = m2.find(n); // return an it for the key 1;
  41. // it == m2.end() ? cout << "not found\n" : cout << "found\n";
  42.  
  43. //using erase(key/it) to erase a particular element from the map.
  44. // map<int,int> m2 = {{1,6},{2,8},{4,9}};
  45. // int n; cin >> n;
  46. // auto it = m2.find(n); // return an it for the key 1;
  47. // if(it != m2.end()) m2.erase(it);
  48. // else cout << "not found\n";
  49. //pMap(m2);
  50.  
  51.  
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
stdout
Standard output is empty