Container MultiMap


Similar to a map container but it can contain duplicate keys. Let’s write a program to store objects of user-defined classes in the multimap container. We shall create two classes named student (with the name as its member) and another class subject (with name and maximum marks as the members). We shall store the names of students along with the subjects that they’ve enrolled for in the multimap container.


//Program using the 'multimap' container to store user-defined objects

#include <iostream>
#include <map>
#include <cstring>
using namespace std;

class student
{
private:
char name[40];

public:
student( )
{
strcpy(name," ");
}

student(char *a)
{
strcpy(name,a);
}

char* str( )
{
return name;
}

};

bool operator <(student a,student b)
{
return strcmp(a.str( ),b.str( ))<0;
}

class subject
{
private:
char name[20];
int maxmarks;

public:
subject( )
{
strcpy(name," ");
maxmarks=0;
}

subject(char *a,int x)
{
strcpy(name,a);
maxmarks=x;
}

char* str( )
{
return name;
}

};

int main( )
{
typedef multimap<student,subject,less<student> > mapsub;
mapsub studsubj;
mapsub::const_iterator ptr;

studsubj.insert(pair<student,subject>(student("john"), subject("biology",100)));
studsubj.insert(pair<student,subject>(student("john"), subject("chemistry",200)));
studsubj.insert(pair<student,subject>(student("ian"), subject("french",100)));

for (ptr=studsubj.begin( );ptr!=studsubj.end( );ptr++)
{
student st=ptr->first;
subject sub=ptr->second;
cout<<endl<<st.str( )<<" "<<sub.str( );
}

return 0;
}

The output will be:

ian french

john biology

john chemistry


The code:

typedef multimap<student,subject,less<student> > mapsub;

mapsub studsubj;

will create a multimap container to hold elements of type student and subject with student being the key and elements ordered in ascending order of student.

Beware: Since the elements will be ordered in ascending order based on student, the class student should have an overloaded < operator (because the container will compare objects using < and decide on the order).

Since student is the key, we need to overload the < operator for the student class.

bool operator <(student a,student b)

{

return strcmp(a.str( ),b.str( ))<0;

}

In this program we just compare the ‘name’ element of the two objects using the strcmp( ) function. The reason for using a.str( ) is because you cannot directly access the private member name( ) using the dot operator.

studsubj.insert(pair<student,subject>(student("john"), subject("biology",100)));

Inserting elements into the container is similar to what we did in the map container. You construct a pair of values of the corresponding data type (in this case it is our own user-defined data type which makes use of the class’ constructor).

for (ptr=studsubj.begin( );ptr!=studsubj.end( );ptr++)

{

student st=ptr->first;

subject sub=ptr->second;

cout<<endl<<st.str( )<<" "<<sub.str( );

}

In this program we just want to display the list of what is stored in the container. ptr->first will contain an object of type ‘student’. We cannot directly code:

cout<<ptr->first;

because we haven’t overloaded the >> operator for the class ‘student’. Thus we need to call the member function str( ) which will return the character array value stored in ‘name’.


Go to Container Adaptors

Go back to Contents page 2.


Copyright © 2004 Sethu Subramanian All rights reserved.