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コンポーネントが含まれています。
ボタンのクリックイベントハンドラには、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 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

