Sort Unordered Set Using STL

C++C++Beginner
Practice Now

Introduction

In this lab, you will learn how to sort an unordered set in C++ programming language. You will learn about the basic concepts of the unordered set and its implementation.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/BasicsGroup -.-> cpp/booleans("`Booleans`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/variables -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/data_types -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/operators -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/booleans -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/conditions -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/for_loop -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} cpp/function_parameters -.-> lab-96215{{"`Sort Unordered Set Using STL`"}} end

Open Terminal & Setup Working Directory

To start with, open the terminal and type the following command to navigate to the project directory:

cd project

Create a C++ file

Now, create a new file called main.cpp in the project directory. This file will contain the code for sorting the unordered set.

touch main.cpp

Write the code

Copy and paste the following code into the main.cpp file.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int x, int y)
{
    if (x > y)
        return true;
    else
        return false;
}

//Function to print the elements of the unordered set using an iterator
void show(unordered_set<int> s)
{
    //declaring an iterator to iterate through the unordered set
    unordered_set<int>::iterator i;

    for (i = s.begin(); i != s.end(); i++)
    {
        cout << *i << "  "; //accessing the elements of the unordered set using * as i stores the address to each element
    }

    cout << endl;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the Sorting of an Unordered Set, in CPP  ===== \n\n\n\n";

    cout << " *** Unordered Set automatically removes the duplicate elements and maintains a random ordering. *** \n\n";

    cout << " *** This random ordering depends on the hash function that is used internally. *** \n\n";

    cout << " *** Unordered set can be sorted by copying its elements to a Vector. *** \n\n";

    //Unordered Set declaration (Unordered Set of integers)
    unordered_set<int> s;

    //Filling the elements by using the insert() method.
    cout << "\n\nFilling the Unordered Set with integers in random order."; //Unlike Set, this is not automatically sorted

    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    cout << "\n\nThe elements of the Unordered Set before sorting are: ";
    show(s);

    //Declaring a vector and initializing it with the elements of the unordered set
    vector<int> v(s.begin(), s.end());

    //Sorting the vector elements in descending order using a custom comparator
    sort(v.begin(), v.end(), cmp);

    cout << "\n\nThe elements of the Unordered Set after sorting in descending Order using a Custom sort method are: \n";

    //declaring an iterator to iterate through the unordered set
    vector<int>::iterator it;

    for (it = v.begin(); it != v.end(); it++)
    {
        cout << *it << "  "; //accessing the elements of the vector using * as i stores the address to each element
    }

    cout << "\n\n\n";

    return 0;
}

Compile and run the code

To compile the program and create an executable file, run the following command in the terminal:

g++ main.cpp -o main

This command will create an executable file named main.

To run the code, execute the following command:

./main

You should see the output of the program in the terminal.

Complete the lab

Copy the final code to the ~/project/main.cpp file:

// ~/project/main.cpp
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int x, int y)
{
    if (x > y)
        return true;
    else
        return false;
}

//Function to print the elements of the unordered set using an iterator
void show(unordered_set<int> s)
{
    //declaring an iterator to iterate through the unordered set
    unordered_set<int>::iterator i;

    for (i = s.begin(); i != s.end(); i++)
    {
        cout << *i << "  "; //accessing the elements of the unordered set using * as i stores the address to each element
    }

    cout << endl;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the Sorting of an Unordered Set, in CPP  ===== \n\n\n\n";

    cout << " *** Unordered Set automatically removes the duplicate elements and maintains a random ordering. *** \n\n";

    cout << " *** This random ordering depends on the hash function that is used internally. *** \n\n";

    cout << " *** Unordered set can be sorted by copying its elements to a Vector. *** \n\n";

    //Unordered Set declaration (Unordered Set of integers)
    unordered_set<int> s;

    //Filling the elements by using the insert() method.
    cout << "\n\nFilling the Unordered Set with integers in random order."; //Unlike Set, this is not automatically sorted

    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    cout << "\n\nThe elements of the Unordered Set before sorting are: ";
    show(s);

    //Declaring a vector and initializing it with the elements of the unordered set
    vector<int> v(s.begin(), s.end());

    //Sorting the vector elements in descending order using a custom comparator
    sort(v.begin(), v.end(), cmp);

    cout << "\n\nThe elements of the Unordered Set after sorting in descending Order using a Custom sort method are: \n";

    //declaring an iterator to iterate through the unordered set
    vector<int>::iterator it;

    for (it = v.begin(); it != v.end(); it++)
    {
        cout << *it << "  "; //accessing the elements of the vector using * as i stores the address to each element
    }

    cout << "\n\n\n";

    return 0;
}

Summary

In this lab, you have learned how to sort an unordered set in C++ programming language. You have learned the basic concepts of the unordered set and its implementation using vector and STL sort method. You have also learned how to declare an unordered set and how to fill it with elements.

Other C++ Tutorials you may like