17 December 2007

STL Set

A set is a simple unique sorted associative container.

It is a simple associative container as the key and the value are the same entities. It is a unique associative container as the values contained in a set are unique. During insertion, if a matching entry is found in the set, the insertion fails. The candidate is matched with the entries using the == operator. Sets are useful for implementing collections that allow fast associative lookup and reject duplicate candidate elements. Sets do not support index-based access of the elements.

Typical methods of STL's set class are listed below. As with the containers discussed above, methods related to iterators are discussed in the next section.

Method

Description

set()

Constructs an empty set.

pair insert(const Key& key)

If the key does not match any of the elements in the collection, the key is inserted otherwise it is rejected. This method returns a pair object the first element of which contains an iterator positioned at the new element and the second element is true in case of a successful insertion.

size_type size(void)

Returns the number of elements contained in the collection.

size_type erase(const Key& key)

Erases the element that matches key and returns the number of elements erased (1 in case of set).

The following example shows insertion and deletion of elements from a set. As access to set elements is only possible via iterators, it will be explained in the next section.

#include 


#include


#include





using namespace std;








void populatePeople(set& peopleSet)


{


char continueFlag = 'y';


string name;


while (continueFlag == 'y')


{


cout << "Enter name "; cin >> name;





if (peopleSet.insert(name).second)


{


cout << continueflag =" cin.get();">& peopleSet)


{


char continueFlag = 'y';


string name;


while (continueFlag == 'y')


{


cout << "Enter name to search "; cin >> name;


if (peopleSet.erase(name) > 0)


{


cout << continueflag =" cin.get();"> peopleSet;


populatePeople(peopleSet);


deleteElements(peopleSet);


return(0);


}


An interaction with this program is shown below,

Enter name Amna


Amna added successfully


1 elements in the set


Enter y to add another, any other key to exit:y


Enter name Omar


Omar added successfully


2 elements in the set


Enter y to add another, any other key to exit:y


Enter name Amna


Amna rejected


2 elements in the set


Enter y to add another, any other key to exit:y


Enter name Saif


Saif added successfully


3 elements in the set


Enter y to add another, any other key to exit:y


Enter name Sameer


Sameer added successfully


4 elements in the set


Enter y to add another, any other key to exit:n


Enter name to search Nadeem


Nadeem not found


Enter y to find another, any other key to exit:y


Enter name to search Sameer


Sameer deleted


3 elements in the set


Enter y to find another, any other key to exit:y


Enter name to search Omar


Omar deleted


2 elements in the set


Enter y to find another, any other key to exit:

No comments: