Thursday 2 April 2009

An example of C++ Multimap

The following is a simple example of C++ Multimap class.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows use of multi-maps
//We input multiple phone numbers for different people
#include<iostream>
#include<map>
#include <string>

using namespace
std;

//forward declaration
void printer(multimap<string, int> pN);

int
main()
{

multimap<string, int> phoneNums;

//Insert key, value as pairs
phoneNums.insert(pair<string, int>("Joe",123));
phoneNums.insert(pair<string, int>("Will",444));
printer(phoneNums);

//Insert duplicates
phoneNums.insert(pair<string, int>("Joe",369));
phoneNums.insert(pair<string, int>("Smith",567));
phoneNums.insert(pair<string, int>("Joe",888));
phoneNums.insert(pair<string, int>("Will",999));
printer(phoneNums);

//Checking frequency of different keys
cout<<"\n\nFrequency of different names"<<endl;
cout<<"Number of Phones for Joe = "<<phoneNums.count("Joe")<<endl;
cout<<"Number of Phones for Will = "<<phoneNums.count("Will")<<endl;
cout<<"Number of Phones for Smith = "<<phoneNums.count("Smith")<<endl;
cout<<"Number of Phones for Zahid = "<<phoneNums.count("Zahid")<<endl;

//Print all Joe from the list and then erase them
pair<multimap<string,int>::iterator, multimap<string,int>::iterator> ii;
multimap<string, int>::iterator it; //Iterator to be used along with ii
ii = phoneNums.equal_range("Joe"); //We get the first and last entry in ii;
cout<<"\n\nPrinting all Joe and then erasing them"<<endl;
for
(it = ii.first; it != ii.second; ++it)
{

cout<<"Key = "<<it->first<<" Value = "<<it->second<<endl;
}

phoneNums.erase(ii.first, ii.second);
printer(phoneNums);

return
0;
}


//This method prints the vector
void printer(multimap<string, int> pN)
{

cout<<"\n\nMultimap printer method"<<endl;
cout<<"Map size = "<<pN.size()<<endl;
multimap<string, int>::iterator it = pN.begin();
while
(it != pN.end())
{

cout<<"Key = "<<it->first<<" Value = "<<it->second<<endl;
it++;
}
}




The output is as follows:

9 comments:

  1. Hi

    Actually i have one question...

    i just want to konw how can i instantiate a multimap if a declare it inside a calss..

    ReplyDelete
  2. You can instantiate it in the constructor or in a method. I will post a simple example later today.

    ReplyDelete
  3. I need to use multi-map in C to store my data. I can't use the C++ Multimap.
    is there any suggestions?

    ReplyDelete
  4. Thanks for this. I need to make use of every one of these examples you've shown. Thanks again

    ReplyDelete
  5. Great blog...good examples which are clear and concise. Keep up the good work.

    ReplyDelete
  6. Can you use class objects as the dataType? e.g.

    multimap borrowedVideos;

    ReplyDelete
  7. Yes you can but not as the way you have described. Suppose you have a class called Videos then you can have
    Videos borrowedVideos;

    multimap is a STL container and you cant use it the way you have written.

    ReplyDelete
  8. Thanks.. A very helpful example.

    ReplyDelete