Associative Containers

The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time. Tom Cargill

hash_multiset

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

0 Comments


Definition

Hash multiset implemented based on hashing with multiset options. Its elements are grouped into buckets based on the value of a hash function in key values.

Character

It is not part of the C++ standard.
Implemented based on hashing with multiset options.
MSDN recommends unordered_multiset instead of hash_multiset.
The insertion order of elements based on the hash function.

source Code

								
#include "stdafx.h"

#include < hash_set >
#include < iostream >
#include < algorithm >
using namespace std;
using namespace stdext;

int _tmain(int argc, _TCHAR* argv[])
{
	hash_multiset < int >::iterator hms0_Iter, hms1_Iter;

	cout << "Create hash multiset and insert the items" << endl;
	hash_multiset < int > hms0;

	cout << "Insert items to hash multiset" << endl;
	for (int i = 0; i < 10; i++)
		hms0.insert(i);

	cout << "Display items from hash multiset: ";
	for (hms0_Iter = hms0.begin(); hms0_Iter != hms0.end(); hms0_Iter++)
		cout << *hms0_Iter << " ";
	cout << endl << endl;
		

	cout << "Create hash_multiset < int, hash_compare < int, less < int > > > and insert the items" << endl;
	hash_multiset < int, hash_compare < int, less < int > > > hms1;
	hms1.insert(12);
	hms1.insert(17);
	hms1.insert(24);
	hms1.insert(17);
	hms1.insert(9);
	
	cout << "Display items from hash multiset: ";
	for (hms1_Iter = hms1.begin(); hms1_Iter != hms1.end(); hms1_Iter++)
		cout << *hms1_Iter << " ";
	cout << endl;
			

	return 0;
}

						

Output

    					 
					Create hash multiset and insert the items
					Insert items to hash multiset
					Display items from hash multiset: 0 8 1 2 3 4 5 6 7 9

					Create hash_multiset < int, hash_compare < int, less < int > > > and insert the items
					Display items from hash multiset: 12 9 17 17 24

					    					 
					    

When to use

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

0 Comments



Please login to add comments.