Modern C++ has a great deal of features and functions designed to make the process of writing apps easier and less labor-intensive. In C++, STL Standard Template Library has many algorithms for some operations like searching, counting, and manipulation of ranges and their elements. C++ has a new feature that you can sort with std::sort Parallel STL Algorithm. C++ has function objects for performing comparisons and they can be used to sort data by std::sort Parallel STL Algorithm with an appropriate C++ Dev Tool that has a C++ compiler.
Table of Contents
What is the std::sort algorithm in C++?
The std::sort
Parallel STL algorithm sorts the elements in the range from the first member to the last member in non-descending order.
The syntax for the std::sort
algorithm can be described as below,
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 objects you can use here,
std::execution::seq
std::execution::par
std::execution::par_unseq
What are the standard library compare function objects in C++?
The standard library compare function objects are used compare data types to be used in other algorithms like sorting algorithms. The standard library compare function objects are included in C++ <functional> library header. Thus, if you want to use them you should add this <functional> library
header to your code.
std::greater
std::greater
is a function object for performing comparisons from higher to lower values. Unless specialized, std::greater
function object invokes operator >
on type T
. Here is the Syntax;
For example we can use it to sort a vector as below,
std::less
std::less
is a function object for performing comparisons from lower to higher values. Unless specialized, std::less
function object invokes operator <
on type T
. Here is the Syntax;
For example we can use it to sort a vector as below,
std::greater_equal
std::greater_equal
is a function object for performing comparisons. Unless specialized, std::greater_equal
function object invokes operator >=
on type T
. Here is the Syntax;
std::less_equal
std::less_equal
is a function object for performing comparisons. Unless specialized, std::less_equal
function object invokes operator <=
on type T
.
How do I sort with the standard library compare function objects in C++?
Sometimes we require very specific sorting techniques when we sort members of data in range. In this case we can use function objects.
Here is the Syntax for the std::sort
method with a function object,
Let’s assume that we want to use std::greater
function object, sort will be as below,
Is there a full example of how to sort with the standard library compare function objects in C++?
Now, let’s see how we can sort a lot of vector elements with function objects, Here is a full C++ example,
Note that, in C++, if you are using arrays, std::array
is on the stack, in other words it has less memory 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.