Unordered Collections

Good code is its own best documentation. As you're about to add a comment, ask yourself, 'How can I improve the code so that this comment isn't needed?' Improve the code and then document it to make it even clearer. Steve McConnell


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



unordered_multiset are equivalent to Unordered Set with one difference. It allows different elements to have equivalent values.


Do not maintain the insertion order and organized into buckets.
The elements in an unordered_multiset cannot be modified.
Elements with equivalent values are grouped together in the same bucket.

source Code

#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
	unordered_multiset < string > myums = { "father", "mother", "son", "daughter", "son", "son" };

	cout << "Display items from unordered_multiset:{";
	for (auto it = myums.begin(); it != myums.end(); ++it)
		cout << *it << " ";
	cout << "}" << endl << endl;

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

	return 0;



					Display items from unordered_multiset:{son son son father mother daughter }

					unordered_multiset buckets contain:
					bucket #0 contains:
					bucket #1 contains: daughter
					bucket #2 contains: mother
					bucket #3 contains: son son son father
					bucket #4 contains:
					bucket #5 contains:
					bucket #6 contains:
					bucket #7 contains:

When to use

unordered_multimap is based on a hash table, which uses hash and equality operations to organize the nodes without sorting them. When you want hash based implementation, you can use unordered_multimap.


Please login to add comments.