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 Kernighan

set

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

0 Comments


Definition

Set is a container which contains sorted set of unique items. Set is implemented as a binary search tree.

Character

Set store the unique elements.
It sort the elements. Sorting is done using the key comparison function Compare.
Set implemented as binary search trees.
It provides a bidirectional iterator to access its elements.

source Code

								
#include "stdafx.h"

#include < iostream >
#include < set >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	set < int > s;
	cout << "Adding number twice" << endl;

	for (int i = 0; i < 10; i++)
		s.insert(i);

	for (int i = 0; i < 10; i++)
		s.insert(i);

	cout << "Set contains:" << endl;
	for (set < int >::iterator it = s.begin(); it != s.end(); ++it)
		cout << ' ' << (*it) << " ";

	cout << endl;

	return 0;
}

						

Output

    					 
					Adding number twice
					Set contains:
					 0  1  2  3  4  5  6  7  8  9
					    					 
					    

When to use

When you want store the unique items, you can use set.

0 Comments



Please login to add comments.