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<const_iterator, bool> 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 <set>
#include <iostream>
#include <string>
using namespace std;
void populatePeople(set<string>& peopleSet)
{
char continueFlag = 'y';
string name;
while (continueFlag == 'y')
{
cout << "Enter name ";
cin >> name;
if (peopleSet.insert(name).second)
{
cout << name << " added successfully" << endl;
}
else
{
cout << name << " rejected" << endl;
}
cout << peopleSet.size() << " elements in the set" << endl;
cout << "Enter y to add another, any other key to exit:";
cin.get();
continueFlag = cin.get();
cin.get();
}
}
void deleteElements(set<string>& peopleSet)
{
char continueFlag = 'y';
string name;
while (continueFlag == 'y')
{
cout << "Enter name to search ";
cin >> name;
if (peopleSet.erase(name) > 0)
{
cout << name << " deleted" << endl;
cout << peopleSet.size() << " elements in the set" << endl;
}
else
{
cout << name << " not found" << endl;
}
cout << "Enter y to find another, any other key to exit:";
cin.get();
continueFlag = cin.get();
cin.get();
}
}
int main(void)
{
set<string> 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: