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.
/*
* C++ Program to Implement Map in Stl
*/
#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
map<char,int> mp;
map<char, int>::iterator it;
int choice, item;
char s;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Map Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Map"<<endl;
cout<<"2.Delete Element of the Map"<<endl;
cout<<"3.Size of the Map"<<endl;
cout<<"4.Find Element at a key in Map"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Count Elements at a specific key"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
cout<<"Enter the key: ";
cin>>s;
mp.insert(pair<char,int>(s ,item));
break;
case 2:
cout<<"Enter the mapped string to be deleted: ";
cin>>s;
mp.erase(s);
break;
case 3:
cout<<"Size of Map: ";
cout<<mp.size()<<endl;
break;
case 4:
cout<<"Enter the key at which value to be found: ";
cin>>s;
if (mp.count(s) != 0)
cout<<mp.find(s)->second<<endl;
else
cout<<"No Element Found"<<endl;
break;
case 5:
cout<<"Displaying Map by Iterator: ";
for (it = mp.begin(); it != mp.end(); it++)
{
cout << (*it).first << ": " << (*it).second << endl;
}
break;
case 6:
cout<<"Enter the key at which number of values to be counted: ";
cin>>s;
cout<<mp.count(s)<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Data Structure Books
- Check Programming Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Design & Analysis of Algorithms MCQ