C++ Program to Illustrate Usage of Map Container

This C++ program illustrates the usage of map container. Maps are associative containers that store elements formed by a combination of a key value and a mapped value. The key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The key of the pair of element can be accessed by dot(‘.’) operator, where “first” is the key value and “second” is the mapped value.

Here is the source code of the C++ program illustrates the usage of map container. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Illustrate usage of map Container
  3.  */ 
  4. #include <iostream>
  5. #include <map>
  6.  
  7. int main()  
  8. {
  9.     std::map <int, std::string> Country;
  10.     std::map <int, std::string>::const_iterator i;
  11.     Country.insert(std::pair <int, std::string>(1, "USA"));
  12.     Country.insert(std::pair <int, std::string>(7, "Russia"));
  13.     Country.insert(std::pair <int, std::string>(33, "France"));
  14.     Country.insert(std::pair <int, std::string>(39, "Italy"));
  15.     Country.insert(std::pair <int, std::string>(49, "Germany"));
  16.     Country.insert(std::pair <int, std::string>(61, "Australia"));
  17.  
  18.     std::cout << "ISD\tCountry " << std::endl;
  19.     std::cout << "---\t--------" << std::endl;
  20.     for (i = Country.begin(); i != Country.end(); i++)
  21.     {
  22.         std::cout << (*i).first << "\t" << (*i).second
  23.                   << std::endl;
  24.     }
  25.     return 0;
  26. }

$ a.out
ISD     Country
---     --------
1       USA
7       Russia
33      France
39      Italy
49      Germany
61      Australia

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.