Simple multimap functions in C++

Multimaps are something that I have found to be extremely valuable. Each map object consists of two different elements: a key, and a value. The multimap uses the key to put he object into order. Once the object is placed, the value can be found by searching for the corresponding key.

A multimap stores a “pair”. Pairs can easily be created using other members of the C++ standard library. This functionality can be added to your program by the following include:

#include <utility>
using std::make_pair;

As usual, you could always use different syntax. You could use either preface every call to make_pair with “std::”, or you could specify that you are using the entire std namespace like this:

using namespace std;

For clarity, I will use the first example.

All it takes to use a multimap is define the multimap, create a pair to put in it, put the pair in the multimap, and read the map out as needed.

Here is a very simple example of using a multimap. Hopefully the code will be self-documenting:

#include <iostream>
using std::cout; using std::endl;

#include <map>
using std::multimap;

#include <string>
using std::string;

#include <utility>
using std::pair; using std::make_pair;

int main(int argc, char** argv)
	multimap<int, string> myMultimap;		// sort by integers
	multimap<string, string> myStringMap;	// sort by strings
	pair<int, string> myPair;				// for clarity
	// The integer determines what the order will be
	// Start by using a "pair" for clarity.
	myPair = make_pair(7, "multimap");
	myPair = make_pair(4, "sample");
	myPair = make_pair(1, "This");
	// then just enter them easily.
	myMultimap.insert(make_pair(6, "a"));
	myMultimap.insert(make_pair(3, "a"));
	myMultimap.insert(make_pair(2, "is"));
	myMultimap.insert(make_pair(5, "of"));
	// create an iterator and read out the multimap
	multimap<int, string>::iterator myIterator;
	for(myIterator = myMultimap.begin();
				myIterator != myMultimap.end();
		cout << myIterator->second << " ";	
	cout << endl;
	// The first string of the pair will be used for order.
	// They will be alphabatized.
	myStringMap.insert(make_pair("dare to be different", "example"));
	myStringMap.insert(make_pair("about", "This"));
	myStringMap.insert(make_pair("clean", "an"));
	myStringMap.insert(make_pair("grown", "strings."));
	myStringMap.insert(make_pair("grape", "sorting"));
	myStringMap.insert(make_pair("bad", "is"));
	myStringMap.insert(make_pair("everybody loves programming", "of"));
	myStringMap.insert(make_pair("green", "by"));
	// create a second iterator and read out the multimap
	multimap<string, string>::iterator stringIterator;
	for(stringIterator = myStringMap.begin();
				stringIterator != myStringMap.end();
		cout << stringIterator->second << " ";	
	cout << endl;
	return 0;

Published by


I am a writer of words, a thinker of thoughts, a changer of genders, and a queerer of life. I am an antagonist of the ordinary; and while I do tolerate it, I also look at it with contempt.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s