Unordered Set in STL C++

About Unordered Set in STL C++:

Unordered Set in STL is defined as a container that stores unique elements in no particular order, and allows for fast retrieval of the elements.

Unordered Set in STL C++

Syntax of Unordered Set :

unordered_set< data_type> unordered_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.
Unordered sets are implemented using a hash table, which means that the elements can be quickly inserted and retrieved, but the order in which the elements are inserted is not preserved.

Unordered sets are useful when you need to store a collection of unique elements and you don’t need to preserve the order of the elements. They offer fast insertion, removal, and search operations, and use less memory than sets because they do not store the elements in a specific order.

Functions in Unordered Set :

 Function Working
insert() It inserts an element to the set.
clear() It removes all the element of the set.
size() It gives the number of elements present in the set.
empty() It tells whether the set is empty or not.
max_size() It returns the maximum number of elements set can hold.

Implementation of Unordered Set in STL C++

Example 1 :

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

int main() {
    unordered_set< int> ust;
    ust.insert(8);
    ust.insert(8);
    ust.insert(3);
    ust.insert(3);
    ust.insert(8);
    cout<<"The elements in the set : ";
    for(auto it : ust){
        cout<< it<<" ";
    }

    return 0;
}

Output :

The elements in the set : 3 8 
In the above program, we take the unordered set ust ,insert 8,8,3,3,8 but only unique elements 3,8 will be inserted in the set and display on the screen.

Example 2:

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

int main() {
    unordered_set< int> ust;
    ust.insert(8);
    ust.insert(8);
    ust.insert(3);
    ust.insert(3);
    ust.insert(8);
    ust.insert(15);
    cout<<"The elements in the set : ";
    for(auto it : ust){
        cout<< it<<" ";
    }
    cout<< endl;
    ust.erase(8);
    cout<<"The remaining elements in the set : ";
    for(auto it : ust){
        cout<< it<<" ";
    }
    cout<< endl;
    cout<<"The size of the set :"<< ust.size();

    return 0;
}

Output :

The elements in the set : 15 3 8 
The remaining elements in the set : 15 3 
The size of the set :2
In the above program, we take the unordered set ust ,insert 8,8,3,3,8,15 but only unique elements 3,8,15 will be inserted in the set and after ust.erase(8) 3, 15 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