SET In STL

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

Quickly Learn SET In STL With Simple Examples.

We will take an in-depth look at the STL container – Sets, here in this tutorial. Sets are associative containers with unique elements in a specific order.

The value of an element in the set is also the key that is used to access it. All elements in the set have to be unique. We cannot modify the elements in the set once they are inserted. However, we can insert or delete the elements.

=> Check Here For Complete C++ FREE Training Series.

SET IN STL

SET In STL

To implement set, we need to include the header <set> in our program.

#include<set>

We can declare a set as follows:

set<datatype> myset;

For Example, if we want a set, myset of an element with integer type, then we can declare the set as:

set<int> myset;

Operations On Set

The set container also supports similar operations like a map which we have already discussed. Following are some of the basic operations supported by set.

  • begin: Returns iterator to the first element of the set.
  • end: Returns iterator to the element that follows the last element of the set.
  • insert: Inserts a new element in the set.

Insert operation for the set has three variants:

      • insert(element): This directly inserts the element in the set and reorders the set.
      • insert(position, hint): Here, we specify the position to insert the element.
      • insert(iterator.begin(), iterator.end()): In this variation, we can directly insert the range into the set like an array or another set.
  • erase: Removes an element from the set.
  • size: Returns the size of the set.
  • max_size: Returns the maximum size that the set can hold.
  • empty: Returns whether the set is empty.
  • clear: Removes all the elements from the set.
  • find: Finds an element in the set. If an element is found, it returns the iterator to that element in the set. If not found, it returns an iterator to the end of the set.

Given below is a program that demonstrates the usage of some important functions of SET.

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

int main()
 {
   set <int> myset;

   myset.insert(140);
   myset.insert(130);
   myset.insert(160);
   myset.insert(120);

   cout<<"\nSize of myset: "<<myset.size();

   set <int > :: iterator itr;
   cout << "\nThe set myset is : ";
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }
   cout << endl;

   set<int>::iterator it = myset.begin();
   myset.insert(it,100);

   cout << "\nAfter inserting 100,the set myset is : ";
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }
   cout << endl;

   int arr[3] = {110,150,150};

   myset.insert(arr,arr+2);
   cout << "\nAfter inserting array arr,the set myset is : ";
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }
   cout << endl;

   cout << "\nAfter removal of elements less than 130, myset: ";

   myset.erase(myset.begin(), myset.find(130));
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }

   cout << endl;

 }

Output:

Size of myset: 4
The set myset is: 120        130             140             160

After inserting 100, the set myset is: 100          120        130       140        160

After inserting array arr, the set myset is:        100       110        120        130       140       150       160

After removal of elements less than 130, myset:           130       140         150       160

set_output

As shown in the output above, we create a set using a simple insert function.

Next, we insert element 100 in the set using another variant of the insert function by passing iterator reference and element value 100. We see that once the insert is done, the set is reordered and the order of elements is maintained.

Next we insert an array {110,150,150} using the insert function. If you see the set output displayed after inserting an array, we see that only one value of 150 is entered into the set. This is because all elements in the set are unique.

We also display the size of the set. Next, using the find function we find the elements that are less than 130 and then call the erase function to remove these elements. Then we display the resultant set.

This is all about the set container. Next, we will discuss multiset which an extension of the set container.

Multiset

A multiset is an associative container similar to set in all aspects except for one difference i.e. multiple elements can have the same value.

The declaration for multiset is as follows:

multiset<int> mset;

A multiset of integer elements can be declared as:

multiset<int> mset;

Various operations supported by multiset are similar to those supported by set.

Now we will directly discuss a multiset Example which demonstrates the operation it uses.

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

int main()
 {
   multiset <int> myset;

   myset.insert(11);
   myset.insert(13);
   myset.insert(13);
   myset.insert(10);

   cout<<"\nSize of myset: "<<myset.size();

   set <int > :: iterator itr;
   cout << "\nAfter inserting four elements, the multiset myset is : ";
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }
   cout << endl;

   set<int>::iterator it = myset.begin();
   myset.insert(it,15);

   cout << "\nAfter inserting 15,the multiset myset is : ";
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }
   cout << endl;

   cout << "\nAfter removal of elements less than 15, myset: ";

   myset.erase(myset.begin(), myset.find(15));
   for (itr = myset.begin(); itr != myset.end(); ++itr)
      { cout << '\t' << *itr; }

   cout << endl;

 }

Output:

Size of myset: 4
After inserting four elements, the multiset myset is: 10         11             13
13

After inserting 15, the multiset myset is: 10         11          13             13             15

After removal of elements less than 15, myset:            15

Screenshot of the output is given below:

multiset_output

As shown in the above output, initially we enter four elements in the multiset out of which two are same. But unlike a set, these elements are successfully inserted in the multiset. Then we insert another element 15 by providing position through an iterator, which is successfully inserted.

Next, we find elements less than 15 in the multiset and call erase function on these elements. Finally, we display the multiset.

Unordered Set

So far we have discussed set and multiset in this tutorial.

While the set is an ordered sequence of unique keys, we have another associative container which is called “unordered set” which is a set of keys or elements that are stored in any order. This means that the elements in the unordered set are ‘unordered’.

Similar to an unordered map, the unordered set is also implemented using a hash table where the keys are hashed into indices of the hash table. Because of the use of a hash table, it is not possible to maintain the order of elements in contrast to the set that uses a balanced tree structure.

The header for implementing unordered set is <unordered_set>.

#include<unordered_set>

We declare an unordered map of type integer as follows:

Unordered_set<int> uset;

Operations supported by unordered_set are similar to those supported by unordered_map which is discussed in our tutorials on map.

Given below is an example that demonstrates the various operations on unordered_set.

#include<iostream>
#include <unordered_set>
using namespace std;
 
int main()
{
	unordered_set<int> uset; 
	unordered_set<int> :: iterator it; 
	
	for(int i=0;i<5;i++){
		uset.insert(i+2); 
	}
	
	cout<<"\nSize of uset: "<<uset.size();
	cout<<endl;
	
	it= uset.begin();
	uset.insert(it,99);
	
	int ary[]= { 13, 26, 39};
	uset.insert(ary, ary+3); // Inserting using method3
	
	cout<<"\nElements in unordered set are: ";
	for(it= uset.begin(); it!=uset.end(); it++) cout << *it << " ";	 
	cout<<endl;
	
	int key=13;
	if (uset.find(key) == uset.end()) 
        		cout<<"\nkey = "<< key << “not found\n\n"; 
    	else
        		cout << "\nFound key = " << key << endl << endl;
	
	cout<<"umap bucket_count : "<<uset.bucket_count();
               cout<<"\nbucket_size : "<<uset.bucket_size(2);
	
	return 0;
}

Output:

Size of uset: 5

Elements in unordered set are: 99 39 6 5 26 4 3 13 2

Found key = 13

umap bucket_count : 11
bucket_size : 2

Screenshot of the above output is given below.

unordered_set

As shown in the above output, we first insert 5 elements in the unordered set and then insert another 4 elements that demonstrate the usage of insert function variations. Then we display the contents of the unordered set.

Next, we make use of find function to find if the key=13 is present in the unordered set or not.

After this, we demonstrate two more functions ‘bucket_count’ and ‘bucket_size’. These functions are related to the internal implementation of the unordered map.

This container also supports the other iterator functions and functions like max_size, clear, erase, empty, etc. that is similar to other STL containers.

Conclusion

With this, we have come to the end of our tutorial on SET in STL.

We hope that the topics covered as a part of this STL tutorials will help you to gain an understanding of STL and its various containers.

=> Read Through The Popular C++ Training Series Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment