Unordered Collections

For a long time it puzzled me how something so expensive, so leading edge, could be so useless. And then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match. Bill Bryson

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.