sidrs

OOP Ass 7 - Maps

Sep 27th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <map>
  4. #include <string>
  5. #include <utility>
  6. using namespace std;
  7.  
  8.  
  9. int main() {
  10.  
  11.     map<string, int> states;
  12.  
  13.     states.insert(pair<string, int>("Maharashtra", 9823456));
  14.     states.insert(pair<string, int>("Uttarakhand", 6801234));
  15.     states.insert(pair<string, int>("Karnataka", 8593589));
  16.     states.insert(pair<string, int>("Delhi", 9123456));
  17.     states.insert(pair<string, int>("Meghalaya", 3410923));
  18.     states.insert(pair<string, int>("Assam", 79812389));
  19.     states.insert(pair<string, int>("Rajasthan", 69239485));
  20.     states.insert(pair<string, int>("Punjab", 1234563));
  21.     states.insert(pair<string, int>("Sikkim", 59234582));
  22.  
  23.     map<string, int>::iterator it;
  24.     for (it = states.begin(); it != states.end(); ++it) {
  25.         cout << left << setw(12) << it->first << ": " << right << setw(8) << it->second << endl;
  26.     }
  27.  
  28.     cout << "\nThe number of States are: " << states.size() << endl;
  29.  
  30.     string name;
  31.     cout << "\nEnter the name of the State: "; cin >> name;
  32.     it = states.find(name);
  33.     if (it != states.end()) {
  34.         cout << "\nMatch Found" << endl;
  35.         cout << setw(12) << left << name << ": " << setw(8) << right << it->second << endl;
  36.         cout << endl;
  37.     } else {
  38.         cout << "\nNo Match Found" << endl;
  39.         cout << endl;
  40.     }
  41.  
  42. }
  43.  
  44.  
  45. /* OUTPUT
  46.  
  47. Assam       : 79812389
  48. Delhi       :  9123456
  49. Karnataka   :  8593589
  50. Maharashtra :  9823456
  51. Meghalaya   :  3410923
  52. Punjab      :  1234563
  53. Rajasthan   : 69239485
  54. Sikkim      : 59234582
  55. Uttarakhand :  6801234
  56.  
  57. The number of States are: 9
  58.  
  59. Enter the name of the State: Sikkim
  60.  
  61. Match Found
  62. Sikkim      : 59234582
  63.  
  64.  
  65.  
  66. Assam       : 79812389
  67. Delhi       :  9123456
  68. Karnataka   :  8593589
  69. Maharashtra :  9823456
  70. Meghalaya   :  3410923
  71. Punjab      :  1234563
  72. Rajasthan   : 69239485
  73. Sikkim      : 59234582
  74. Uttarakhand :  6801234
  75.  
  76. The number of States are: 9
  77.  
  78. Enter the name of the State: Kerala
  79.  
  80. No Match Found
  81.  
  82. */
  83.  
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment