Mastering C++ Unordered Sets with STL
Tags: coding, programming, tutorial
Published: 2024-07-11 09:51:10


## Introduction

This article covers the following tech skills:

![Skills Graph](https://skills-graph.labex.io/cpp-c-using-stl-unordered-set-96234.jpg)

In [this lab](https://labex.io/tutorials/cpp-c-using-stl-unordered-set-96234), 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.

```bash
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.

```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 << " ";
    }
}
```


## Fill the unordered set with integers

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

```cpp
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.

```cpp
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:

```bash
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`:

```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](https://labex.io/tutorials/cpp-c-using-stl-unordered-set-96234), 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](https://internal-api-drive-stream.feishu.cn/space/api/box/stream/download/authcode/?code=MDNlMDcyM2YxYTAwNWNiOTAwNzE5MTdjNGYwYWU5YzJfMjVkM2NiY2Q5ZDEyODEzNmM4NDRkM2NmYTJiYTkzY2FfSUQ6NzM5MDMxMzUxNjkxNjExMzQxMV8xNzIwNjkxNDY4OjE3MjA3Nzc4NjhfVjM)

---

> 🚀 Practice Now: [C++ Using STL Unordered Set](https://labex.io/tutorials/cpp-c-using-stl-unordered-set-96234)

---

## Want to Learn More?

- 🌳 Learn the latest [C++ Skill Trees](https://labex.io/skilltrees/cpp)
- 📖 Read More [C++ Tutorials](https://labex.io/tutorials/category/cpp)
- 💬 Join our [Discord](https://discord.gg/J6k3u69nU6) or tweet us [@WeAreLabEx](https://twitter.com/WeAreLabEx)