Associative Containers

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. Eric Raymond

hash_set

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

0 Comments


Definition

Hash set is implemented as a hash table. Hash set is an extension which support to store and retrieve the data from collection faster.

Character

hash_set is an extension that is not part of the C++ standard.
hash_set inserted random in the set.
It implemented as a hash table. So, its elements must have a unique key.
It provide greater efficiency due to hash based implementation.

source Code

								
#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
	hash_set < int > hs;
	hash_set < int >::iterator hs_Iter;
	
	cout << "Insert items to hashset" << endl;
	for (int i = 0; i < 10;i++)
		hs.insert(i);

	hs_Iter = hs.begin();
	cout << "The first element of hash set is " << *hs_Iter << endl;

	hs_Iter = hs.begin();
	cout << "Erase first element from hash set " << *hs_Iter << endl;
	hs.erase(hs_Iter);

	cout << "Display items from hash set: ";
	for (hs_Iter = hs.begin(); hs_Iter != hs.end(); hs_Iter++)
		cout << *hs_Iter << " ";
	cout << endl;

	return 0;
}

						

Output

    					 
					Insert items to hashset
					The first element of hash set is 0
					Erase first element from hash set 0
					Display items from hash set: 8 1 2 3 4 5 6 7 9 
					    					 
					    

When to use

Hash set is implemented as a hash table. When you want set implementation based on hash functionality, you can use hash set.

0 Comments



Please login to add comments.