fork download
  1. //
  2. // main.cpp
  3. // Map and Unordered Map (STL)
  4. //
  5. // Created by Himanshu on 23/03/22.
  6. //
  7.  
  8. #include <iostream>
  9. #include <map>
  10. #include <unordered_map>
  11. #include <string>
  12. using namespace std;
  13.  
  14. void printUnorderedMap (unordered_map<string, int> myMap) {
  15. for (auto& it: myMap) {
  16. cout<<(it.first)<<": "<<(it.second)<<" ";
  17. }
  18. cout<<endl;
  19. }
  20.  
  21. void printMap (map<string, int> myMap) {
  22. for (auto& it: myMap) {
  23. cout<<(it.first)<<": "<<(it.second)<<" ";
  24. }
  25. cout<<endl;
  26. }
  27.  
  28. int main () {
  29.  
  30. unordered_map<string, int> initUnorderedMap = { {"beta", 2}, {"gamma", 3}, {"alpha", 1} };
  31. map<string, int> initMap = { {"beta", 2}, {"gamma", 3}, {"alpha", 1} };
  32.  
  33. unordered_map<string, int> myUnorderedMap;
  34. map<string, int> myMap;
  35.  
  36. map<string, int>::iterator it;
  37.  
  38. //Iterating over 'unordered_map'
  39. cout<<"initUnorderedMap elements:"<<endl;
  40. printUnorderedMap(initUnorderedMap);
  41.  
  42. //Iterating over 'map'
  43. cout<<endl<<"initMap elements:"<<endl;
  44. printMap(initMap);
  45.  
  46. cout<<"Elements in the initMap (map) container are kept sorted based on their key values"<<endl;
  47.  
  48. initUnorderedMap.clear();
  49. //size of unordered_map after clear
  50. cout<<endl<<"Size of unordered_map after clear: "<<initUnorderedMap.size()<<endl;
  51.  
  52. initMap.clear();
  53. //size of map after clear
  54. cout<<endl<<"Size of map after clear: "<<initMap.size()<<endl;
  55.  
  56. myUnorderedMap.insert({"first", 30});
  57. myUnorderedMap["second"] = 20;
  58. myUnorderedMap["third"] = 10;
  59.  
  60. myMap.insert({"first", 10});
  61. myMap["second"] = 20;
  62. myMap["third"] = 30;
  63.  
  64. //Iterating over 'myUnorderedMap'
  65. cout<<endl<<"myUnorderedMap elements:"<<endl;
  66. printUnorderedMap(myUnorderedMap);
  67.  
  68. //Iterating over 'myMap'
  69. cout<<endl<<"myMap elements:"<<endl;
  70. printMap(myMap);
  71.  
  72. //Iterating over 'myUnorderedMap' after erase
  73. myUnorderedMap.erase("second");
  74. cout<<endl<<"myUnorderedMap elements after erase 'second': "<<endl;
  75. printUnorderedMap(myUnorderedMap);
  76.  
  77. //Iterating over 'myMap' after erase
  78. myMap.erase(myMap.begin());
  79. cout<<endl<<"myMap elements after erase myMap.begin(): "<<endl;
  80. printMap(myMap);
  81. cout<<endl<<"Find operation on map and unordered_map:"<<endl;
  82.  
  83. if (myUnorderedMap.find("second") != myUnorderedMap.end()) {
  84. cout<<"key 'second' is present in myUnorderedMap"<<endl;
  85. } else {
  86. cout<<"key 'second' is not present in myUnorderedMap"<<endl;
  87. }
  88.  
  89. if (myMap.find("second") != myMap.end()) {
  90. cout<<"key 'second' is present in myMap"<<endl;
  91. } else {
  92. cout<<"key 'second' is not present in myMap"<<endl;
  93. }
  94.  
  95. myMap.clear();
  96.  
  97. myMap["a"] = 1;
  98. myMap["b"] = 2;
  99. myMap["c"] = 3;
  100. myMap["d"] = 4;
  101. myMap["e"] = 5;
  102. myMap["f"] = 6;
  103. myMap["g"] = 7;
  104. myMap["h"] = 8;
  105.  
  106. //Iterating over 'myMap'
  107. cout<<endl<<"myMap new elements after clear:"<<endl;
  108. printMap(myMap);
  109.  
  110. //Iterating over 'myMap' after erase on a range
  111. myMap.erase(myMap.find("f"), myMap.end());
  112. cout<<endl<<"myMap elements after erasing a range of elements:"<<endl;
  113. printMap(myMap);
  114.  
  115. //Upper bound of "c" in myMap
  116. cout<<endl<<"(Upper bound method is available for map but not for unordered_map) Upper bound of 'c' in myMap: ";
  117. cout<<((myMap.upper_bound("c"))->first)<<": "<<((myMap.upper_bound("c"))->second)<<endl;
  118.  
  119. //Lower bound of "c" in myMap
  120. it = myMap.lower_bound("c");
  121. cout<<endl<<"(Lower bound method is available for map but not for unordered_map) Lower bound of 'c' in myMap: ";
  122. cout<<it->first<<": "<<it->second<<endl;
  123.  
  124. return 0;
  125. }
Success #stdin #stdout 0.01s 5372KB
stdin
Standard input is empty
stdout
initUnorderedMap elements:
gamma: 3 alpha: 1 beta: 2 

initMap elements:
alpha: 1 beta: 2 gamma: 3 
Elements in the initMap (map) container are kept sorted based on their key values

Size of unordered_map after clear: 0

Size of map after clear: 0

myUnorderedMap elements:
third: 10 first: 30 second: 20 

myMap elements:
first: 10 second: 20 third: 30 

myUnorderedMap elements after erase 'second': 
third: 10 first: 30 

myMap elements after erase myMap.begin(): 
second: 20 third: 30 

Find operation on map and unordered_map:
key 'second' is not present in myUnorderedMap
key 'second' is present in myMap

myMap new elements after clear:
a: 1 b: 2 c: 3 d: 4 e: 5 f: 6 g: 7 h: 8 

myMap elements after erasing a range of elements:
a: 1 b: 2 c: 3 d: 4 e: 5 

(Upper bound method is available for map but not for unordered_map) Upper bound of 'c' in myMap: d: 4

(Lower bound method is available for map but not for unordered_map) Lower bound of 'c' in myMap: c: 3