logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement Map in STL

Posted on October 11, 2013 by Manish
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.

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Implement Next_Permutation in STL
» Next Page - C++ Program to Implement LeftList Heap
« C++ Program to Implement Network_Flow Problem
C++ Program to Implement LeftList Heap »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Set & String Problems & Algorithms
  2. Java Programming Examples on Data-Structures
  3. C++ Programming Examples on Data-Structures
  4. C Programming Examples on Linked List
  5. Java Programming Examples on Collection API
  6. C Programming Examples on Data-Structures
  7. C++ Programming Examples on STL
  8. C Programming Examples on Searching and Sorting
  9. C# Programming Examples on Interfaces
  10. C Programming Examples on Stacks & Queues
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry