DEV Community

Cover image for Mastering C++ Unordered Sets with STL
Labby for LabEx

Posted on

Mastering C++ Unordered Sets with STL

Introduction

This article covers the following tech skills:

Skills Graph

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.

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
Enter fullscreen mode Exit fullscreen mode

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 << " ";
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Compile and run the code

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

g++ main.cpp -o main && ./main
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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.

MindMap


🚀 Practice Now: C++ Using STL Unordered Set


Want to Learn More?

Top comments (0)