In this tutorial, we are going to learn about the set container in C++ standard template library. The set is an associative container in which elements should be unique, that is set container doesn’t accept multiple values to store in it. In the set container, values are stored in some specific order, either ascending or descending order.

The syntax of the set container is.

                                        set<datatype> set_name;

We can store any kind of data in the sets like int, float, char, double, etc. We can directly assign values to a set by

  1. giving values enclosed between curly braces {} set<int> val = {6, 10, 5, 1};
  2. 2. Using the insert() function to assign values to a set.

Let’s look at the different properties of the set container.

  • The set stores the elements in sorted order.
  • All the elements in the set should be unique. Multiple values cannot be assigned.
  • The values of the set are immutable. Although we can add or remove values from the set, the value of the element cannot be modified once it is added to the set.
  • Elements stored in sets are doesn’t have any index values.
  • Set container follows binary search tree implementation.

let’s look at some of the basic methods that are implemented on the set container.

begin(): this method returns the first element in the set.

end(): this method returns the last element in the set.

size(): returns the size of the set, which means the number of elements in the set.

See also  Passing Parameters to a Function in C++

max_size(): this max_size function returns the maximum capacity of elements that a set can hold.

empty(): this method returns a boolean value whether the set is empty or not.

Insert(const g): this method is used to add a new element ‘g’ to the set.

erase(const g):  this method is used to remove the element ‘g’ from the set.

 Insertion and deletion of elements in the set are associated with the time complexities of O( logN ).

Let’s look at a sample program implementing the set container.

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
main() {
    set<int> s1;
    s1.insert(50);  // adding 50 two times 
    set<int>::iterator itr;
    cout << "Set s1 is : \n";
    for (itr = s1.begin(); itr != s1.end(); itr++) {
        cout << *itr << " ";

The output of the program is.

In the above example, we created a set that contains integer values and after creating a set, the insertion of values into the set is done by using the insert() method. 50 is inserted into the set two times but as a set doesn’t accept duplicate values, it has removed the multiple occurrences of the 50. after that, we created an iterator and made that iterator traverse over the set.

This is the implementation of the set container.

There is a slight difference between the set and the unordered set. Now, what is an unordered set?

An unordered set is the same as a normal set but the elements stored in the unordered set don’t follow any specific sorted order, that is elements get stored in the order in which they are inserted.

See also  High-Level vs Low-Level Language

Let’s look at the differences between the normal set and the unordered set.

                               SET                     UNORDERED SET 
The set stores elements in some specific sorted order.An unordered set stores elements in the order in which they are inserted.
The set stores unique elements only.An unordered set also stores unique elements like set.
The set follows binary search tree implementation.An unordered set follows hash tables for implementation.
Syntax: set<datatype> set_name;Syntax:Unordered_set< datatype > unordered


That’s it from this tutorial. We have learned about the set container, its properties, different kinds of basic methods implemented on sets, and the differences between normal set and unordered-set. Hope you guys found it interesting.

Happy Coding!

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.