Set in STL C++

About Set in STL :

Set in STL is defined as the container used to store unique elements in the particular order either increasing or decreasing order.

peterson's algorithm for critical section problem in operating system

Syntax of Set :

set< data_type> set_name;

In STL, each value in the set serves as a key and the set doesn’t enable indexing, each element of the set is distinct, meaning that no duplicate values may be placed in it. Since there can only be one index, the elements/values (keys) themselves are the indexes. Additionally, just the keys and values must be used to access the values in the set. The components of the Set are likewise kept in sorted order.

In logarithmic time complexity, the element in the set may be added, deleted, and searched. As soon as an element is added to a set, it becomes a constant and cannot be changed (its value cannot be altered). The binary search tree implements the set STL internally in C++ (Red-Black Tree).

Functions in Set :

 FunctionWorking
insert()insert an element to the set.
clear()remove all the element of the set.
size()give the number of elements present in the set.
empty()tells whether the set is empty or not.
max_size()

return the maximum number of elements set can hold.

Example of  Set in STL :

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

int main() {
    set< int> st;
    st.insert(5);
    st.insert(5);
    st.insert(1);
    st.insert(1);
    st.insert(5);
    cout<<"The elements in the set : ";
    for(auto it : st){
        cout<< it<<" ";
    }

    return 0;
}

Output :

The elements in the set : 1 5

In the above program, we take the set st ,insert 5, 5, 1,1,5, 10 but only unique elements 1,5, 10 will inserted in the set and display on the screen.

Example of Set in STL:

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

int main() {
    set< int> st;
    st.insert(5);
    st.insert(5);
    st.insert(1);
    st.insert(1);
    st.insert(5);
    st.insert(10);
    cout<<"The elements in the set : ";
    for(auto it : st){
        cout<< it<<" ";
    }
    cout<< endl;
    st.erase(5);
    cout<<"The remaining elements in the set : ";
    for(auto it : st){
        cout<< it<<" ";
    }
    cout<< endl;
    cout<<"The size of the set :"<< st.size();

    return 0;
}

Output :

The elements in the set : 1 5 10 
The remaining elements in the set : 1 10 
The size of the set :2

In the above program, we take the set st ,insert 5, 5, 1,1,5, 10 but only unique elements 1,5, 10 will inserted in the set and after st.erase(5) 1, 10 remained in the set.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription