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,
1 2 3 |
void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last, <function object> ); // since C++14 |
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;
1 2 3 |
template< class T = void > struct greater |
For example we can use it to sort a vector as below,
1 2 3 4 5 |
std::vector<int> vec(100); std::sort( vec.begin(), vec.end(), std::greater<int>() ); |
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;
1 2 3 |
template< class T = void > struct less; |
For example we can use it to sort a vector as below,
1 2 3 4 5 |
std::vector<int> vec(100); std::sort( vec.begin(), vec.end(), std::less<int>() ); |
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;
1 2 3 |
template< class T = void > struct greater_equal; |
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
.
1 2 3 |
template< class T = void > struct less_equal; |
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,
1 2 3 |
void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last, <function object> ); // since C++14 |
Let’s assume that we want to use std::greater
function object, sort will be as below,
1 2 3 4 5 |
std::vector<int> vec(100); std::sort( vec.begin(), vec.end(), std::greater<int>() ); |
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,
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 <vector> #include <algorithm> #include <ctime> int main() { std::vector<int> vec(100000); // fill vector with random numbers std::srand(unsigned(std::time(nullptr))); std::generate(vec.begin(), vec.end(), std::rand); // sort vector std::sort( vec.begin(), vec.end(), std::greater<int>() ); // print first 100 and last 100 members to check if sorted well for(auto it = vec.begin(); it<vec.begin()+100; ++it) std::cout << *it << ','; std::cout << '\n'; for(auto it = vec.end()-100; it<vec.end(); ++it) std::cout << *it << ','; std::cout << '\n'; system("pause"); return 0; } |
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.