Using set and multiset in c++

One of the main problems of using an array is that it has a set size that is defined when it is compiled. Because of this, the code can’t change. There is no way to increase the size of the array if you need to put more elements in it than it was designed to hold. C++ offers us a solution to this problem.

Edit: I am specifically talking about ordered sets, not arrays. Ordered sets are more akin to ordered arrays.

One of the “#include” files in the standard library is called “set”. This file can be included by adding the following line at the beginning of your code:

#include <set>

If you don’t want to prefix every command for programming a set with “std::”, you can make the following “using” declaration:

#include <set>
using std::set;

I like to do it this way since that doesn’t hide the substitution behind using a complete namespace. With that done, it is as simple as creating a set “variable”, putting things in it, and reading its contents.

To define set, simply tell it what kind of object it is working with. In the following example I will be working with integers, therefore the creation will look like this:

set<int> numberSet;

Here, numberSet is the name of the “array” set of integers that I will be using. Integers can be added to the set like this:

numberSet.insert(x);

where x is a valid integer.

The contents of the set can be read by using an iterator that acts like a pointer into an array. You define the iterator, point it where you want it to go, then use the content operator to read the contents:

int theNumber;
set<int>::iterator x;
x = numberSet.begin();
theNumber = *x;

The iterator can be increased and decreased using the standard increase and decrease nomenclature:

++x;
x++;
--x;
x--;

As long as the explanation for the code was, the actual code is quite easy. Here is the complete c++ file for a simple set:

#include <iostream>
using std::cout; using std::endl;

#include <set>
using std::set;

int main(int argc, char** argv)
{
	set<int> numberSet;				// An "array" without any length!
	numberSet.insert(3);				// Numbers can be added
	numberSet.insert(5);				// without setting up
	numberSet.insert(2);				// an array of any kind.
	numberSet.insert(7);				// The set will grow as needed
	numberSet.insert(1);				// letting us concentrate
	numberSet.insert(9);				// on other programming.
	
	numberSet.insert(3);				// These numbers won't be inserted
	numberSet.insert(5);				// again since the "set<>" class
	numberSet.insert(7);				// doesn't allow duplicates.
	numberSet.insert(9);				// Use multiset for duplicates.
	
	for(set<int>::iterator x = numberSet.begin(); x != numberSet.end(); ++x)
	{
		cout << *x << " ";
	}
	cout << endl;
	return 0;
}

Edit: It is important to note that when the number is added to the set, it is automatically placed in the correct order. This, or course, is why it is actually referred to as an ordered set.

If you want to be able to have more that one copy of each integer in the set, all you have to do is substitute “multiset” for “set”. Here is the complete source code:

#include <iostream>
using std::cout; using std::endl;

#include <set>
using std::multiset;

int main(int argc, char** argv)
{
	multiset<int> numberSet;			// An "array" without any length!
	numberSet.insert(3);				// Numbers can be added
	numberSet.insert(5);				// without setting up
	numberSet.insert(2);				// an array of any kind.
	numberSet.insert(7);				// The set will grow as needed
	numberSet.insert(1);				// letting us concentrate
	numberSet.insert(9);				// on other programming.
	
	numberSet.insert(3);				// These number will be
	numberSet.insert(5);				// added a second time in this
	numberSet.insert(7);				// example. If you don't want
	numberSet.insert(9);				// duplicates, use set<>.
	
	for(multiset<int>::iterator x = numberSet.begin(); x != numberSet.end(); ++x)
	{
		cout << *x << " ";
	}
	cout << endl;
	return 0;
}
Advertisements

Published by

Marisa

I am a writer of words, a thinker of thoughts, a changer of genders, and a queerer of life. I am an antagonist of the ordinary; and while I do tolerate it, I also look at it with contempt.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s