Set Container


Set is a simple, sorted, unique associative container. Attempting to insert a duplicate key will not lead to an error; it will simply be ignored by the container. If you are creating a word game in which you want to check whether a particular word exists in the list then you only need to store the words (i.e. you only need to keep track of the keys since you won’t be needing anything else). Whenever the user forms a word, all you need to do is use the find( ) member function to see if the word exists in the container.

There are many algorithms which you can use on containers but we’ll be dealing with algorithms later on.

Let’s check out an example program that uses the set container.

//Program using the 'set' container

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

int main( )
{
typedef set<string,less<string> > stringset;
stringset strset1;
stringset::const_iterator ptr;

string a("test");
strset1.insert(a);
strset1.insert(a);
strset1.insert("testing");
strset1.insert("apple");
strset1.insert("sun");

cout<<endl<<"The container has the following words:";

for (ptr=strset1.begin( ); ptr!=strset1.end( ); ptr++)
{
cout<<endl<<*ptr;
}

ptr = strset1.find("test");
if (ptr!=strset1.end( ))
{
cout<<endl<<"Found the word test";
}

cout<<endl<<"Size of the container: "<<strset1.size( );
return 0;
}

The output will be:

The container has the following words:

apple
sun
test
testing

Found the word test
Size of the container: 4

To make use of the set and multiset containers you need to include the <set> header file. A typedef has been used to avoid retyping the entire set declaration again.

typedef set<string,less<string> > stringset;

                    stringset strset1;

The part

                    set<string,less<string> >

says that we are declaring a set which will contain strings and is arranged in ascending order based on the string value. The data type of elements you use in the set container should support the < comparison operator. Hence if you are using a set container to hold your own user-defined data types, then you should have provided with an overloaded < operator function for that data type. The code:

strset1.insert(a);

                    strset1.insert(a);

should insert the same string twice but as you can see in the output this string is stored only once in the container. Such an operation does not cause an error. The code:

ptr = strset1.find("test");

if (ptr!=strset1.end( ))

checks to see if the string "test" is present in the container. If it is found in the container then the find( ) function will return the iterator corresponding to the position where the element was found. If the element is not present in the container then find( ) will return a reference to end of the container (i.e. a reference to a position beyond the last element of the container).

Remember: the end( ) function does not return an iterator to the last element; it returns an iterator past the last element.

The rest of the program uses the normal member functions that we have already discussed. Do take note of the output of the program. As you can see, the words are stored in ascending order (irrespective of their order of insertion).


Go to the next section on: MultiSet Container

Go back to Contents page 2.


Copyright © 2004 Sethu Subramanian All rights reserved.