Unordered Collections

Before software can be reusable it first has to be usable. Ralph Johnson

unordered_multimap

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

0 Comments


Definition

Unordered multimaps are equivalent to unordered map containers with different elements to have equivalent keys property.

Character

The items do not sort and organized into buckets.
The items accessed based on the hash key value.
The map store the key and a value. The value can be duplicated.

source Code

								
#include "stdafx.h"

#include < iostream >
#include < string >
#include < unordered_map >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "insert itmes into unordered_multimap " << endl;
	unordered_multimap < string, string > myumm = {
		{ "apple", "red" },
		{ "apple", "green" },
		{ "orange", "orange" },
		{ "strawberry", "red" }
	};

	cout << "Display items from unordered_multimap :" << endl;
	for (auto it = myumm.begin(); it != myumm.end(); ++it)
		cout << "{" << it->first << "," << it->second << "} " ;
	cout << endl << endl;

	cout << "unordered_multimap buckets contains:\n";
	for (unsigned i = 0; i < myumm.bucket_count(); ++i) {
		cout << "bucket #" << i << " contains: ";
		for (auto local_it = myumm.begin(i); local_it != myumm.end(i); ++local_it)
			cout << "{" << local_it->first << "," << local_it->second << "} ";
		cout << endl;
	}

	return 0;
}

						

Output

    					 
					insert itmes into unordered_multimap
					Display items from unordered_multimap :
					{apple,red} {apple,green} {orange,orange} {strawberry,red}

					unordered_multimap buckets contains:
					bucket #0 contains:
					bucket #1 contains:
					bucket #2 contains:
					bucket #3 contains: {orange,orange}
					bucket #4 contains: {strawberry,red}
					bucket #5 contains:
					bucket #6 contains:
					bucket #7 contains: {apple,red} {apple,green}
					    					 
					    

When to use

unordered_multimap elements with same key are stored in the same bucket if the implementation is a bucket hash map.

0 Comments



Please login to add comments.