Open In App

Set in C++ STL

Last Updated : 21 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sets are associative container which stores unique elements in some sorted order. By default, it is sorted ascending order of the keys, but this can be changed as per requirement.

Syntax

The set container is defined as std::set class template inside <set> header file.

set<T, comp> s;

where,

  • T: Data type of elements in the set.
  • s: Name assigned to the set.
  • comp: It is a binary predicate function that tells set how to compare two elements. It is used to sort set in custom order. It is optional and if not provided, set is sorted in increasing order.

Declaration and Initialization

We can declare and initialize a set in multiple ways as shown in the below code:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // Creating an empty set
    set<int> s1;

    // Creating a set from an initializer list
    set<int> s2 = {5, 1, 3, 2, 4};

    for (auto i : s2) 
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Basic Operations

Here are the basic operations that can be performed on a set:

Inserting Elements

In set, elements are added using the insert() or emplace() function. If the element already exists in the set, it will not be added again.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {1, 4, 2};
    
    // Insert elements into set
    s.insert(5);
    s.emplace(3);
    s.insert(5);
    
    for (auto x: s) cout << x << " ";
    return 0;
}

Output
1 2 3 4 5 

Accessing Elements

We can't access elements using an index like in arrays or vectors. Instead, we use an iterator starting from begin() and move it using ++ or functions like next() or advance() to reach a specific position.
This is because sets store elements in sorted order without direct indexing.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {1, 4, 2, 3, 5};

    // Accessing first element
    auto it1 = s.begin();
    
    // Accessing third element
    auto it2 = next(it1, 2);
    
    cout << *it1 << " " << *it2;
    return 0;
}

Output
1 3

Updating Elements

We cannot change the value of elements once they are stored in the set.

Finding Elements

Set provides fast search by value operation using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {1, 4, 2, 3, 5};

    // Finding 3
    auto it = s.find(3);
    
    if (it != s.end()) cout << *it;
    else cout << "Element not Found!";
    return 0;
}

Output
3

Traversing

Just like other containers, sets can be easily traversed using range-based for loop or using begin() and end() iterators.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {5, 1, 4, 3, 2};
    
    // Traversing using range based for loop
    for(auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
    return 0;
}

Output
1 2 3 4 5 

Deleting Elements

In set, elements are removed from a set using the erase() function. We can erase elements either by value or by position.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {1, 4, 2, 3, 5};
    
    // Deleting elements by value
    s.erase(5);
    
    // Deleting first element by iterator
    s.erase(s.begin());
    
    for (auto x: s) cout << x << " ";
    return 0;
}

Output
2 3 4 

empty()

This checks whether the set has any elements. It returns true if the set is empty, otherwise false.

C++
#include<bits/stdc++.h>
using namespace std;

int main(){
    //Create an empty set of integers
    set<int>mySet;
    //check if the set is empty
    if(mySet.empty()){
        cout<<"The set is empty. "<<endl;
    }
    //Insert an element
    mySet.insert(10);
    if(!mySet.empty()){
        cout<<"The set is not empty now. "<<endl;
    }
    return 0;
}

Output
The set is empty. 
The set is not empty now. 

Size of Set

The size() function in a set returns the number of elements currently in the set. Since sets store only unique elements, the size reflects the count of distinct values.

C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    set<int>mySet;
    mySet.insert(20);
    mySet.insert(10);
    cout<<"Size of the set: "<<mySet.size()<<endl;
    
    return 0;
}

Output
Size of the set: 2

Time Complexity

The below table lists the time complexity of the above operations on set:

OperationTime Complexity
Insert an elementO(log n)
Delete an elementO(log n)

Find the largest element

O(1)

Find smallest element

O(1)

Find element by valueO(log n)
Traverse the setO(n)

Set vs Unordered Set

Following is the primary differences between set and unordered_set in C++:

Feature

set

unordered_set

Ordering

Elements are sorted

Elements are not sorted

Underlying Data Structure

Balanced BST(e.g Red-Black Tree)

Hash Table

Time Complexity

O(log n) for insert/search

O(1) average, O(n) worst case

Duplicates

Not allowed

Not allowed

Use Case

When order matters

When speed matters



Set in C++ STL
Visit Course explore course icon
Practice Tags :

Similar Reads