Associative Containers

First, solve the problem. Then, write the code. John Johnson


Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-17 04:45:46



hash_multimap helps to store and retrieve the key and value pair elements. hash_multimap class is a bidirectional iterator.


Storage and fast retrieval of data from a collection.
Store and retrieve the key and value pair elements.
It uses bidirectional iterator to iterate both sides.

source Code

#include "stdafx.h"

#include < hash_map >
#include < iostream >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
	typedef pair < int, int > Int_Pair;
	hash_multimap < int, int >::iterator  hmp_Iter;
	hash_multimap < int, int, hash_compare < int, less < int > > > hmp;

	cout << "Insert items into hash_multimap" << endl;
	for (int i = 0; i < 10;i++)
		hmp.insert(Int_Pair(i, i*3));
	cout << "Display items from hash_multimap " << endl;
	for (hmp_Iter = hmp.begin(); hmp_Iter != hmp.end(); hmp_Iter++)
		cout << "Key:"<< hmp_Iter->first << " Value:" << hmp_Iter->second << endl;
	cout << endl;
	return 0;



					Insert items into hash_multimap
					Display items from hash_multimap
					Key:0 Value:0
					Key:8 Value:24
					Key:1 Value:3
					Key:2 Value:6
					Key:3 Value:9
					Key:4 Value:12
					Key:5 Value:15
					Key:6 Value:18
					Key:7 Value:21
					Key:9 Value:27

When to use

When you want hash over sorting in greater efficiency, you can use Hash multimap.


Please login to add comments.