C++ Program to Implement Map in STL

This C++ Program demonstrates implementation of Map in Stl.

Here is source code of the C++ Program to demonstrate Map in Stl. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Map in Stl
  3.  */
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     map<char,int> mp;
  12.     map<char, int>::iterator it;
  13.     int choice, item;
  14.     char s;
  15.     while (1)
  16.     {
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"Map Implementation in Stl"<<endl;
  19.         cout<<"\n---------------------"<<endl;
  20.         cout<<"1.Insert Element into the Map"<<endl;
  21.         cout<<"2.Delete Element of the Map"<<endl;
  22. 	cout<<"3.Size of the Map"<<endl;
  23.         cout<<"4.Find Element at a key in Map"<<endl;
  24.         cout<<"5.Dislplay by Iterator"<<endl;
  25.         cout<<"6.Count Elements at a specific key"<<endl;
  26.         cout<<"7.Exit"<<endl;
  27.         cout<<"Enter your Choice: ";
  28.         cin>>choice;
  29.         switch(choice)
  30.         {
  31.         case 1:
  32.             cout<<"Enter value to be inserted: ";
  33.             cin>>item;
  34.             cout<<"Enter the key: ";
  35.             cin>>s;
  36.             mp.insert(pair<char,int>(s  ,item));
  37.             break;
  38.         case 2:
  39.             cout<<"Enter the mapped string to be deleted: ";
  40.             cin>>s;
  41.             mp.erase(s);
  42.             break;
  43.         case 3:
  44. 	    cout<<"Size of Map: ";
  45. 	    cout<<mp.size()<<endl;
  46.             break;
  47.         case 4:
  48. 	    cout<<"Enter the key at which value to be found: ";
  49.             cin>>s;
  50.             if (mp.count(s) != 0)
  51.                 cout<<mp.find(s)->second<<endl;
  52.             else
  53.                 cout<<"No Element Found"<<endl;
  54.             break;
  55.         case 5:
  56. 	    cout<<"Displaying Map by Iterator: ";
  57.             for (it = mp.begin(); it != mp.end(); it++)
  58.             {
  59.                 cout << (*it).first << ": " << (*it).second << endl;
  60.             }
  61.             break;
  62.         case 6:
  63.             cout<<"Enter the key at which number of values to be counted: ";
  64.             cin>>s;
  65.             cout<<mp.count(s)<<endl;
  66.             break;
  67. 	case 7:
  68.             exit(1);
  69. 	    break;
  70.         default:
  71.             cout<<"Wrong Choice"<<endl;
  72.         }
  73.     }
  74.     return 0;
  75. }

$ g++ map.cpp
$ a.out
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 1
Enter the key: a
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2
Enter the key: b
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 3
Enter the key: c
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
Enter the key: d
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 5
Enter the key: e
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 3
Size of Map: 5
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 4
Enter the key at which value to be found: a
1
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
c: 3
d: 4
e: 5
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 2
Enter the mapped string to be deleted: c
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
d: 4
e: 5
 
---------------------
Map Implementation in Stl
 
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 7
 
 
------------------
(program exited with code: 1)
Press return to continue

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

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

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.