Unordered Collections

Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to to, let us concentrate rather on explaining to human beings what we want a computer to do. Donald Knuth

Unordered Set

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

0 Comments


Definition

Unordered sets are container that store the unique items without any order.

Character

Store the unique items without any order.
The order of elements in the controlled sequence depends on the hash function.
Keys are immutable and unique. The items organized into buckets depending on their hash values.

source Code

								
#include "stdafx.h"

#include < iostream >
#include < string >
#include < unordered_set >
using namespace std;
using namespace stdext;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Initialize the items in unordered_set" << endl << endl;
	unordered_set < string > myset = { "tiger", "lion", "puma", "cheetah", "leopard", "cougar", "ocelot" };

	cout << "Display items from unordered_set:";
	for (auto it = myset.begin(); it != myset.end(); ++it)
		cout << *it << " ";
	cout << endl << endl;

	cout << "unordered_set buckets contain:\n";
	for (unsigned i = 0; i < myset.bucket_count(); ++i) {
		cout << "bucket #" << i << " contains:";
		for (auto local_it = myset.begin(i); local_it != myset.end(i); ++local_it)
			cout << " " << *local_it;
		cout << endl;
	}


	return 0;
}

						

Output

    					 
					Initialize the items in unordered_set

					Display items from unordered_set:puma tiger lion cheetah leopard cougar ocelot

					unordered_set buckets contain:
					bucket #0 contains: puma tiger
					bucket #1 contains: cheetah
					bucket #2 contains: leopard
					bucket #3 contains:
					bucket #4 contains: cougar
					bucket #5 contains: ocelot
					bucket #6 contains:
					bucket #7 contains: lion

					    					 
					    

When to use

When you need write hash functions to store and find your own types, you can use unordered_set.

0 Comments



Please login to add comments.