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.
/*
* C++ Program to Illustrate usage of map Container
*/
#include <iostream>
#include <map>
int main()
{
std::map <int, std::string> Country;
std::map <int, std::string>::const_iterator i;
Country.insert(std::pair <int, std::string>(1, "USA"));
Country.insert(std::pair <int, std::string>(7, "Russia"));
Country.insert(std::pair <int, std::string>(33, "France"));
Country.insert(std::pair <int, std::string>(39, "Italy"));
Country.insert(std::pair <int, std::string>(49, "Germany"));
Country.insert(std::pair <int, std::string>(61, "Australia"));
std::cout << "ISD\tCountry " << std::endl;
std::cout << "---\t--------" << std::endl;
for (i = Country.begin(); i != Country.end(); i++)
{
std::cout << (*i).first << "\t" << (*i).second
<< std::endl;
}
return 0;
}
$ 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.
Related Posts:
- Practice Programming MCQs
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check Programming Books
- Practice Computer Science MCQs