fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. // Initializing the map
  9. map<string, int> myMap;
  10. myMap["Bangladesh"] = 300;
  11. myMap["Italy"] = 50;
  12. myMap["Canada"] = 100;
  13. myMap["America"] = 200;
  14.  
  15. // Declaring the iterator
  16. map<string, int>::iterator it;
  17.  
  18. // Iterating through the map and printing keys and elements
  19. for(it = myMap.begin(); it != myMap.end(); it++) {
  20. cout << "Key : " << it->first;
  21. cout << " and Element : " << it->second << endl;
  22. }
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Key : America and Element : 200
Key : Bangladesh and Element : 300
Key : Canada and Element : 100
Key : Italy and Element : 50