Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++ニュース

C++Builder 10.4 Sydney の Win32およびWin64で、C++17 アルゴリズムライブラリ並列ソートを使用する

C++Builder 10.4 Sydneyは、Win32およびWin64用のClangベースのコンパイラでISO C++17標準をサポートしています。C++17標準には、並列処理をサポートするための実行ポリシーを提供するアルゴリズムライブラリが含まれています。 この記事ではC++ std::vector およびアルゴリズムライブラリのソートと並列実行ポリシーを使用して、vector内のランダムな整数を並べ替える簡単なVCLの例を示します。 この例は、32ビットおよび64ビットのWindows用Clangベースコンパイラを使用してコンパイルされています。

VCLフォームには、TButton、TLabel、および2つのTMemoコンポーネントが含まれています。

pastedimage1597072022306v1-3966634

ボタンのクリックイベントハンドラには、vectorを作成し、並べ替えて結果を表示するための簡単なコードが含まれています。

#include 
#include 

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    const int max_data = 1000;   // number of random numbers to create
    Memo1->Lines->Clear();
    Memo2->Lines->Clear();
    Label1->Caption = "Building Random Data";
    Label1->Update();

    // fill the vector with random numbers and save them in Memo1
    std::vector my_data;

    for (int i = 1; i <= max_data; i++) {
        int random_value = Random(max_data);
        my_data.push_back(random_value);
        Memo1->Lines->Add(IntToStr(random_value));
    }
    Label1->Caption = "Sorting Random Data";
    Label1->Update();

    // sort the random numbers in the vector
    std::sort(std::execution::par,my_data.begin(),my_data.end());

    // put the sorted vector in Memo2
    Label1->Caption = "Sorting Completed";
    Label1->Update();
    for(int n : my_data) {
        Memo2->Lines->Add(IntToStr(n));
    }
}

もし非ClangとClangコンパイラのコードを混在させたい場合は、アプリケーションで #if、#elif、#else、#endif 条件コンパイル指令を使用できます。

#if defined(__clang__)
    #if (__clang_major__ == 5 && __clang_minor__ == 0)
        #warning "clang major = 5 and clang minor = 0"
    #elif (__clang_major__ == 3 && __clang_minor__ == 3)
        #warning "clang major = 3 and clang minor = 3"
    #else
        #warning "Unable to determine correct clang header version"
    #endif
#else
    #warning "not a clang compiler"
#endif

このシンプルな例で使用されているC++17標準のリファレンス:

std::vector

C++ コンテナライブラリ std::vector

ヘッダー <vector> で定義
https://ja.cppreference.com/w/cpp/container/vector

アルゴリズムライブラリ

アルゴリズムライブラリは、標準のC++ライブラリを超える機能を提供します。 このライブラリは、検索、サンプリング、ソート、計数、操作、数値演算などの追加の関数が定義されています。

ヘッダー <algorithm>で定義
https://ja.cppreference.com/w/cpp/algorithm

ソートアルゴリズム
https://ja.cppreference.com/w/cpp/algorithm/sort

アルゴリズム実行ポリシー
https://ja.cppreference.com/w/cpp/algorithm/execution_policy_tag

アルゴリズム実行ポリシー型
https://ja.cppreference.com/w/cpp/algorithm/execution_policy_tag_t

RAD Studio 13.2 Florence Now Available! Kai 1.1.1 Now Available! What's Coming in RAD Studio 13.2 Florence

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES