STL を使って無順序セットをソートする

C++C++Beginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、C++ プログラミング言語で無順序セットをソートする方法を学びます。無順序セットの基本概念とその実装について学びます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/FunctionsGroup(["Functions"]) cpp(("C++")) -.-> cpp/IOandFileHandlingGroup(["I/O and File Handling"]) cpp(("C++")) -.-> cpp/StandardLibraryGroup(["Standard Library"]) cpp(("C++")) -.-> cpp/SyntaxandStyleGroup(["Syntax and Style"]) cpp(("C++")) -.-> cpp/BasicsGroup(["Basics"]) cpp/BasicsGroup -.-> cpp/operators("Operators") cpp/FunctionsGroup -.-> cpp/function_parameters("Function Parameters") cpp/IOandFileHandlingGroup -.-> cpp/output("Output") cpp/IOandFileHandlingGroup -.-> cpp/user_input("User Input") cpp/IOandFileHandlingGroup -.-> cpp/files("Files") cpp/StandardLibraryGroup -.-> cpp/standard_containers("Standard Containers") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("Code Formatting") subgraph Lab Skills cpp/operators -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/function_parameters -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/output -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/user_input -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/files -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/standard_containers -.-> lab-96215{{"STL を使って無順序セットをソートする"}} cpp/code_formatting -.-> lab-96215{{"STL を使って無順序セットをソートする"}} end

ターミナルを開き、作業ディレクトリを設定する

まずは、ターミナルを開き、次のコマンドを入力して project ディレクトリに移動します。

cd project

C++ ファイルを作成する

次に、project ディレクトリに新しいファイル main.cpp を作成します。このファイルには、無順序セットをソートするコードが含まれます。

touch main.cpp

コードを記述する

次のコードをコピーして 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;
}

コードをコンパイルして実行する

プログラムをコンパイルして実行可能ファイルを作成するには、ターミナルで次のコマンドを実行します。

g++ main.cpp -o main

このコマンドにより、main という名前の実行可能ファイルが作成されます。

コードを実行するには、次のコマンドを実行します。

./main

ターミナルにプログラムの出力が表示されるはずです。

実験を完了する

最終的なコードを ~/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;
}

まとめ

この実験では、C++ プログラミング言語で無順序セットをソートする方法を学びました。無順序セットの基本概念と、ベクターと STL の sort メソッドを使ったその実装方法を学びました。また、無順序セットを宣言する方法と、要素で埋める方法も学びました。