C++ Using STL Unordered Set

C++C++Beginner
Practice Now

Introduction

In this lab, you will learn how to implement and use std::unordered_set in C++. A set is used to store unique values of a list and sort them automatically. An unordered set is similar to a set, except it does not sort the elements and stores them in a random order. It also automatically removes any duplicated elements.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/output -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} cpp/variables -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} cpp/data_types -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} cpp/operators -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} cpp/for_loop -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} cpp/function_parameters -.-> lab-96234{{"`C++ Using STL Unordered Set`"}} end

Set up the project directory

First, create a project folder to contain your code. Open the terminal and navigate to the folder using the cd command.

cd ~/project
touch main.cpp

Create a new file called main.cpp using any text editor of your choice.

Create a program to demonstrate the working of Unordered Sets

In this step, write a program to demonstrate the working of std::unordered_set in C++. This program will declare an empty std::unordered_set, fill it with some elements, delete an element, and then print the elements of the set.

Start by including the necessary libraries and creating a show function to print the elements of the unordered set using an iterator.

#include <iostream>
#include <unordered_set>

void show(std::unordered_set<int> s)
{
    std::unordered_set<int>::iterator it;

    for (it = s.begin(); it != s.end(); ++it)
    {
        std::cout << *it << " ";
    }
}

Fill the unordered set with integers

In this step, fill the std::unordered_set with six integers using the insert method.

int main()
{
    std::unordered_set<int> s;
    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    std::cout << "The elements of the unordered set are: \n";
    show(s);

    return 0;
}

Delete an element from the unordered set

In this step, delete an element from the unordered set using the erase method. Then, print the updated set.

int main()
{
    std::unordered_set<int> s;
    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    std::cout << "The elements of the unordered set are: \n";
    show(s);

    s.erase(39);
    std::cout << "\nAfter deleting the element 39 from the unordered set using the erase() method, it becomes: \n";
    show(s);

    return 0;
}

Compile and run the code

To compile and run the code, use the following command in the terminal:

g++ main.cpp -o main && ./main

The output will be:

The elements of the unordered set are:
54 35 5 64 39 82
After deleting the element 39 from the unordered set using the erase() method, it becomes:
54 35 5 64 82

Full main.cpp code

Here is the full code for main.cpp:

#include <iostream>
#include <unordered_set>

void show(std::unordered_set<int> s)
{
    std::unordered_set<int>::iterator it;

    for (it = s.begin(); it != s.end(); ++it)
    {
        std::cout << *it << " ";
    }
}

int main()
{
    std::unordered_set<int> s;
    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    std::cout << "The elements of the unordered set are: \n";
    show(s);

    s.erase(39);
    std::cout << "\nAfter deleting the element 39 from the unordered set using the erase() method, it becomes: \n";
    show(s);

    return 0;
}

Summary

In this lab, you learned how to implement and use std::unordered_set in C++. std::unordered_set is used to store unique values and removes any duplicates automatically. Unlike std::set, it does not sort the elements and stores them in a random order.

Other C++ Tutorials you may like