Title: To use map associative container.
Problem Statement: Write a program in C++ to use map associative container. The keys will
be the names of states and the values will be the populations of the
states. When the program runs, the user is prompted to type the name
of a state. The program then looks in the map, using the state name as
an index and returns the population of the
state
Prerequisites:
Object Oriented Programming
Objectives: To learn the concept of map associative container.
Theory:
Map associative con
Map associative cont
element has a key va
This operator is used
to the at() function, th
the position is not in
Syntax :
tainer:
ainer are associative containers that store elements in a mapped fashion.
Each lue and a mapped value. No two mapped values can have same key
values.
map::operator[]
to reference the element present at position given inside the operator. It
is similar e only difference is that the at() function throws an out-ofrange exception when the bounds of the size of map, while this operator
causes undefined behaviour.
mapname[key] Parameters :
Key value mapped to the element to be fetched.
Returns :
Direct reference to the element at the given key value.
Examples:
Input : map mymap; mymap['a'] = 1;
mymap['a']; Output : 1
Input : map mymap; mymap["abcd"] = 7;
mymap["abcd"];
Output : 7
//Program
#include <map>
#include <iostream>
#include<string>
Using namespace std;
Int main()
{
// map declaration map<int,string>mymap;
// mapping integers to strings mymap[1] = "Hi";
mymap[2] = "This";
mymap[3] = "is";
mymap[4] = "NBN";
// using operator[] to print string
// mapped to integer 4 cout<<mymap[4]; return0;
}
Output:
NBN
Facilities:
Linux Operating Systems, G++
Algorithm:
1. Start.
2. Give a header file to map associative container.
3. Insert states name so that we get values as population of that state.
4. Use populationMap.insert().
5. Display the population of states.
6. End.
Input: Information such as state name to map associative container.
Output:
Size of population Map: 5
Brasil: 193 million
China: 1339 million
India: 1187 million
Indonesia: 234 million
Pakistan: 170 million
Indonesia's populations is 234 million
Conclusion: Hence, we have successfully studied the concept of map associative container
Questions:
1. What is an associative container in C++?
2. What is map in C++?
3. How to do declare a map?
4. Explain Associative mapping with example?
0 Comments