C++ Program to Implement Set in STL



Set is an associative container that stores unique elements in a sorted order. In this article, we will learn how to use the set container from the Standard Template Library (STL) in C++.

What is Set?

A Set is a container that stores data in a sorted order without any duplicates. Meaning, the duplicate values are automatically eliminated, and the elements are kept in ascending order by default. The STL library of C++ provides a pre-defined set container that uses a balanced binary search tree for storage.

For example, in the code we have shown how data are inserted and stored in set:

// Declare a set
set<int> s;
// Add Data
s.insert(10);
s.insert(20);
s.insert(10); // This will not be added as 10 is already present

Using set Container in STL

The set container is defined in the <set> header of STL. It provides fast access and automatically sorts the elements. Below are some points about this container:

  • Header: <set>
  • Syntax:
    set<datatype> set_name;
  • Common functions:
  • insert() - Function used to insert an element into the set.
  • erase() - Function used to remove an element from the set.
  • find() - Function used to search for an element in the set.
  • count() - Function used to count the number of occurrences of an element (will be 0 or 1 in set).
  • empty() - Function used to check if the set is empty.
  • size() - Function used to return the number of elements in the set.

Steps to Implement Set in C++ STL

Following are steps/algorithm to use set using C++ STL:

  • Include the <set> header file.
  • Declare a set with desired data type.
  • Use insert() to add elements.
  • Use erase() to remove elements.
  • Use find() to check if an element exists.
  • Use empty() and size() to check status.

C++ Program to Implement Set using STL

The below code is the implementation of the above algorithm in C++ language.

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

int main() {
    set<int> s;

    // Insert elements
    s.insert(30);
    s.insert(10);
    s.insert(20);
    s.insert(10); // Duplicate, will be ignored

    cout << "Set Elements: ";
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;

    // Check if element exists
    if (s.find(20) != s.end()) {
        cout << "20 is present in set" << endl;
    }

    // Remove an element
    s.erase(10);

    cout << "After erasing 10, Set Elements: ";
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;

    cout << "Set Size: " << s.size() << endl;

    if (s.empty()) {
        cout << "Set is empty" << endl;
    } else {
        cout << "Set is not empty" << endl;
    }

    return 0;
}

The output of above code will be

Set Elements: 10 20 30
20 is present in set
After erasing 10, Set Elements: 20 30
Set Size: 2
Set is not empty

Time and Space Complexity

Time Complexity:

  • insert(), erase(), find(): O(log n) for each operation.

Space Complexity: O(n), where n is the number of elements in the set.

Updated on: 2025-05-12T19:45:46+05:30

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements