Articles Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64 by David I

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64
David I - Monday, August 10 2020
[SHOWTOGROUPS=4,20,22]

C++Builder 10.4 Sydney supports the Для просмотра ссылки Войди или Зарегистрируйся. Part of the C++17 standard includes the Для просмотра ссылки Войди или Зарегистрируйся that provides execution policies to support parallel operations. Below you will find a simple VCL example that uses the C++ std::vector and the algorithms library sort and parallel execution policy to sort random integers in the vector. This example currently compiles using the Clang base compilers for 32 and 64 bit Windows.

My VCL form contains a TButton, TLabel and two TMemo components.



The Button on-click event handler contains the simple code to create the vector, sort it and display the results.

Код:
#include <algorithm>
#include <vector>

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<int> 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));
    }
}

If you want to include code for non-Clang and Clang compilers, you can use the following #if, #elif, #else, #endif preprocessor directives in your applications.

Код:
#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 References used in this simple example:

std::vector


C++ Containers library std::vector
Defined in header <vector>
Для просмотра ссылки Войди или Зарегистрируйся


Algorithms library


The algorithms library adds functionality beyond the standard C++ library. This library defines additional functions for searching, sampling, sorting, counting, manipulating, generalized summing and more.
Defined in header <algorithm>
Для просмотра ссылки Войди или Зарегистрируйся


Sort algorithm
Для просмотра ссылки Войди или Зарегистрируйся


Algorithm execution policies
Для просмотра ссылки Войди или Зарегистрируйся


Для просмотра ссылки Войди или Зарегистрируйся
Для просмотра ссылки Войди или Зарегистрируйся
[/SHOWTOGROUPS]