fork(2) download
  1. /*
  2. created by Rian Adam Rajagede
  3. 8 Agustus 2020
  4. */
  5.  
  6. #include <iostream>
  7. #include <map>
  8. #include <unordered_map>
  9. using namespace std;
  10.  
  11. map<string, int> m;
  12. unordered_map<string, int> u;
  13.  
  14. int main() {
  15. cout << "=====================Section A======================" << endl;
  16. cout << "====================================================" << endl;
  17.  
  18. int age[5] = {17, 15, 18};
  19.  
  20. cout << "Before insert:" << endl;
  21. cout << "Standard Map M size:" << m.size() << endl;
  22. cout << "Unordered Map M size:" << u.size() << endl;
  23.  
  24. // insert
  25. m["candi"] = age[0];
  26. m["budi"] = age[1];
  27. u["candi"] = age[0];
  28. u["budi"] = age[1];
  29.  
  30. cout << "After insert:" << endl;
  31. cout << "Standard Map M size:" << m.size() << endl;
  32. cout << "Unordered Map M size:" << u.size() << endl;
  33.  
  34. cout << "=====================Section B======================" << endl;
  35. cout << "====================================================" << endl;
  36.  
  37. // search
  38. cout << "'candi' value from m: " << m["candi"] << endl;
  39. cout << "'candi' value from u: "<< u["candi"] << endl;
  40.  
  41. cout << "=====================Section C======================" << endl;
  42. cout << "====================================================" << endl;
  43.  
  44. // check before insert
  45. if(!m["adi"]){
  46. m["adi"] = age[2];
  47. }
  48. if(!u["adi"]){
  49. u["adi"] = age[2];
  50. }
  51. if(!m["candi"]){
  52. m["candi"] = -100;
  53. }
  54. cout << "Standard Map M size:" << m.size() << endl;
  55. cout << "Unordered Map M size:" << u.size() << endl;
  56.  
  57. cout << "=====================Section D======================" << endl;
  58. cout << "====================================================" << endl;
  59.  
  60. map<string, int>::iterator im;
  61. unordered_map<string, int>::iterator iu;
  62.  
  63. cout << "Iterate over m" << endl;
  64. for(im=m.begin();im!=m.end();im++){
  65. cout << "key: " << im->first << ", value: " << im->second << endl;
  66. }
  67.  
  68. cout << "Iterate over u" << endl;
  69. for(iu=u.begin();iu!=u.end();iu++){
  70. cout << "key: " << iu->first << ", value: " << iu->second << endl;
  71. }
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
=====================Section A======================
====================================================
Before insert:
Standard  Map M size:0
Unordered Map M size:0
After insert:
Standard  Map M size:2
Unordered Map M size:2
=====================Section B======================
====================================================
'candi' value from m: 17
'candi' value from u: 17
=====================Section C======================
====================================================
Standard  Map M size:3
Unordered Map M size:3
=====================Section D======================
====================================================
Iterate over m
key: adi,  value: 18
key: budi,  value: 15
key: candi,  value: 17
Iterate over u
key: adi,  value: 18
key: candi,  value: 17
key: budi,  value: 15