Loading....
Coupon Accepted Successfully!

 

Maps

A map is a series of pairs of key names and values associated with it as per Figure 20.4. Access of data values depends on the key, and it is very quick. We should specify the key to get the corresponding value.

128024.png

Fig.  20.4 Key and values in maps

 

20.16 Write a program to manipulate list of item names and their code numbers using maps.

#include<iostream>

#include<map>

#include<string>

using namespace std;

typedef map<string,int> item_map;

int main()

{

int sz;

string item_name;

int codeno;

item_map item;

cout<<“Enter item name and code no number for 2 items: \n”;

for (int i=0;i<2;i++)

{

cin>>item_name;

cin>>codeno;

item[item_name]=codeno;

}

item[“PC”]=2510;

item.insert(pair<string,int> (“printer”,2211));

sz=item.size();

cout<<“\n Size of map:”<<sz <<“\n\n”;

cout<<“List of item name and code numbers \n”;

item_map::iterator t;

for ( t=item.begin();t!=item.end();t++)

cout<<(*t).first <<“ <<(*t).second<<“\n”;cout<<“\n”;

cout<<“Enter item name:”;

cin>>item_name;

codeno=item[item_name];

cout<<“Code Number:”<<codeno <<“\n”;

return 0;

}

 

OUTPUT

Enter item name and code no number for 2 items:

TV 51

CD 52

SIZE OF MAP :4

LIST OF ITEM NAME AND CODE NUMBERS

CD 52

PC 2510

TV 51

printer 2211

Enter item name : PC

Code Number : 2510

 

 

Explanation: In the above program, using typedef statement item_map, a map variable is created. The first for loop and cin statement read two records through the keyboard. Using the member function insert() and assignment, two records are inserted. The size() member function returns the number of records. The variable t is an iterator to the map. The second for loop and the iterator produce the list of item names and code numbers. At the end, the program displays a code number that is associated with the entered item name. The item_name is a key. The list is automatically arranged in sorted order.

A map is generally also known as an associative array. The key is given with the help of the subscript operator [ ] in the following manner:

item[“PC”]=2510;

The above statement creates a record for “PC” and links it with code number 2510. The codeno is an object. It is also possible to insert and remove pairs at any point in the map. The insert() delete() function performs these tasks. Frequently used functions are described in Table 20.10.

 

20.17 Write a program to clear map using clear() function.

#include<iostream>

#include<map>

#include<string>

using namespace std;

typedef map<string,int> item_map;

int main()

{

int sz;

string item_name;

int codeno;

item_map item;

item[“PC”]=2510;

item.insert(pair<string,int> (“printer”,2211));

sz=item.size();

cout<<“\n Size of map:”<<sz <<“\n\n”;

item.clear();

sz=item.size();

cout<<“\n after clear() Size of map:”<<sz <<“\n\n”;

return 0;

}

OUTPUT

Size of map :2

after clear() Size of map :0

 

 

 

Explanation: In the above program, two records are added in the map item_map. Using the size() function, the size (total number of records) is displayed; that is, the clear() function clears the contents of the map. After the clear() function, the size of the map becomes zero.

Table 20.9 describes important functions of map class.

Table 20.9 Important functions of the map class

Function

Task

clear( )

Removes all elements from the map

begin( )

Provides references of the first element

erase( )

Removes the given elements

insert( )

Inserts the elements as given

empty( )

Determines whether the map is vacant or not

end( )

Provides references to the end of the map

size( )

Provides the size of the map

find( )

Provides the location of the given elements

swap( )

Swaps the elements of the given map with those of the calling map





Test Your Skills Now!
Take a Quiz now
Reviewer Name