C++ STL Set Container – std::set

In this tutorial you will learn about STL Set container in C++ i.e. std::set and all functions applicable on it.

Set is a associative container. We know that in associative containers each element is unique. So sets are also containers that stores unique elements following in a specific order. The word associative means each value associated with a key value. For any kind of operation key will be more preferred than actual value. Here sets are special type of associative containers where value itself is key value.

Some more facts about set are, elements in set are constant. It means that we are unable to modify once we insert the element. If we want to update element then we should delete that element and again insert with updated element. The elements in the set are always sorted.

C++ STL Set

Let see some functions associated with sets:

Before working with functions let see iterators that can apply on list to manipulate the data in list.

begin(): returns iterator to the beginning

end(): returns iterator to the end of the list

rbegin(): returns reverse iterator to reverse beginning

rend(): returns reverse iterator to reverse end.

These iterators we can use in our programs.

First thing we need to include is set header file. Which is #include<set>

Inserting element into set:

There are different ways we can insert elements into set.

Note: In any method below when we insert an element into set it automatically inserted at proper position based on ascending sorted order.

Method 1: Insert directly by passing element.  setName.insert(element);

Method 2: Using iterator. This returns iterator at inserted position. setName.insert (iterator,value)

Method 3: Copying from another container.

Example program for inserting into set:

Output

0 10 20 23 30 34 40 45 56 99

Some more functions applicable on set are:

erase(): We can erase an element by specifying value or pointing to iterator.

swap(): swaps elements of set1 to set2 and set2 to set1.

clear(): removes all elements in the list. It results list of size 0.

Example program to show usage of above functions:

Output

deleting element 12 –> 10 11 13 14
set1 elements before swapping –> 10 11 13 14
set2 elements before swapping –> 0 1 2 3
set1 elements after swapping –> 0 1 2 3
set2 elements after swapping –> 10 11 13 14
list is empty 

Some more functions are:

empty(): returns a Boolean value whether set is empty or not.

size(): returns the size of the list.

max_size(): returns the maximum size a set can have.

find(): It returns iterator to the element.

count(x):  Returns how many times elements “x” present in set.

Example program to show usage of above functions:

Output

list is not empty
size of the list is 5
maximum size of the list is 461168601842738790
finding elemnt 12 in list
12
22 is not in the list

Comment below if you have any queries or found any information incorrect in above tutorial for STL Set container in C++.

2 thoughts on “C++ STL Set Container – std::set

  1. Ashwin Kushwah

    Pls explain the conut(): function
    You have mentioned that it Returns how many times elements “x” present in set. But set conains all different elements you elements can present at most one time in set.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *