C++ has really great features that comes with its modern libraries. In C++, STL Standard Template Library has many algorithms for some operations like searching, counting, and manipulation of ranges and their elements. C++17 has a new feature that you can sort vectors with std::sort Parallel STL Algorithm. Vectors and arrays can be used and sorted by std::sort Parallel
STL Algorithm with an appropriate C++ Development Tool.
What is std::sort algorithm in C++?
std::sort Parallel STL Algorithm sorts the elements in the range from the first member to the alst memeber in non-descending order. During the order of equal elements, it is not guaranteed to be preserved.
Syntax for the std::sort algorithm can be described as below,
1 2 3 |
void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last ); // since C++17 |
As an ExecutionPolicy std::execution::seq is used for the sequential execution, std::execution::par is used for the parallel execution . In general, there are 3 ExecutionPolicy you can use here,
std::execution::seq
std::execution::par
std::execution::par_unseq
Now let’s see how we can use std::sort() with std::array in examples,
How to sort std::array with parallel STL sorting algorithm in C++?
In C++, after the C++11 standards, an array
(std::array)
is a container that encapsulates fixed size arrays. This is a C++11 array that can be used in other C++ versions like C++14, C++17, C++20, etc. You just need to include <array>
header as below.
1 |
#include <array> |
std::array
is one of the sequence containers of Containers Library which also has vector
, deque
, list
, set
, map
, etc. std::array
is an aggregate type with the same semantics as a struct holding a C style arrays as its only non-static data member. Unlike a C-style array, std::array doesn’t decay a pointer automatically. As an aggregate type, it can be initialized with aggregate-initialization given at most N
initializers that are convertible to T
ype. For example, a std::array
can be used as below,
1 2 3 |
std::array<object type, <number of elements> > variable_name; |
we can create an ar array with 100 members as bleow,
1 2 3 |
std::array<int, 100> ar; |
simply we can sort this array as below,
1 2 3 |
std::sort( ar.begin(), ar.end() ); |
Is there a full example of how to sort std::array with parallel STL sorting algorithm?
Now let’s see how we can sort a lot of vector elements with std::sort algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <array> #include <algorithm> #include <ctime> int main() { std::array<int, 100000> ar; // fill array with random numbers std::srand(unsigned(std::time(nullptr))); std::generate(ar.begin(), ar.end(), std::rand); // sort array std::sort(ar.begin(), ar.end()); // print first 100 and last 100 members to check if sorted well for(auto it = ar.begin(); it<ar.begin()+100; ++it) std::cout << *it << ','; std::cout << '\n'; for(auto it = ar.end()-100; it<ar.end(); ++it) std::cout << *it << ','; std::cout << '\n'; system("pause"); return 0; } |
We can add execution policy with std::execution::par
execution policy as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <array> #include <algorithm> #include <ctime> int main() { std::array<int, 100000> ar; // fill array with random numbers std::srand(unsigned(std::time(nullptr))); std::generate(ar.begin(), ar.end(), std::rand); // sort array std::sort(std::execution::par, ar.begin(), ar.end()); // print first 100 and last 100 members to check if sorted well for(auto it = ar.begin(); it<ar.begin()+100; ++it) std::cout << *it << ','; std::cout << '\n'; for(auto it = ar.end()-100; it<ar.end(); ++it) std::cout << *it << ','; std::cout << '\n'; system("pause"); return 0; } |
Note that, in C++ std::array
is on the stack, in other words it has less limits than vectors.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition