Associative Containers

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. Brian W. Kernighan

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.