Ordenar Conjunto Não Ordenado Usando STL

C++Beginner
Pratique Agora

Introdução

Neste laboratório, você aprenderá como ordenar um conjunto desordenado na linguagem de programação C++. Você aprenderá sobre os conceitos básicos de um conjunto desordenado (unordered set) e sua implementação.

Abrir Terminal e Configurar Diretório de Trabalho

Para começar, abra o terminal e digite o seguinte comando para navegar até o diretório project:

cd project

Criar um arquivo C++

Agora, crie um novo arquivo chamado main.cpp no diretório project. Este arquivo conterá o código para ordenar o conjunto desordenado.

touch main.cpp

Escrever o código

Copie e cole o seguinte código no arquivo 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 LabEx :-)\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;
}

Compilar e executar o código

Para compilar o programa e criar um arquivo executável, execute o seguinte comando no terminal:

g++ main.cpp -o main

Este comando criará um arquivo executável chamado main.

Para executar o código, execute o seguinte comando:

./main

Você deverá ver a saída do programa no terminal.

Completar o laboratório

Copie o código final para o arquivo ~/project/main.cpp:

// ~/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 LabEx :-)\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;
}

Resumo

Neste laboratório, você aprendeu como ordenar um conjunto não ordenado (unordered set) na linguagem de programação C++. Você aprendeu os conceitos básicos de um conjunto não ordenado e sua implementação usando um vetor e o método sort da STL. Você também aprendeu como declarar um conjunto não ordenado e como preenchê-lo com elementos.