In C++, the Standard Template Library or STL has many algorithms for operations like searching, counting, and manipulation of ranges and their elements. C++17 has a new feature that you can sort with std::sort
parallel STL algorithm. Lambda Expressions are another great feature of C++ and they can be used to sort data by std::sort
parallel STL algorithm. Make sure you’re using a modern C++ Code Editor and compiler so that you get the benefits of the C++17 improvements.
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. If it sorts equal elements can’t be guaranteed that their positions will be preserved.
The 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 parallel execution. In total, there are 3 ExecutionPolicy values you can use here:
std::execution::seq
std::execution::par
std::execution::par_unseq
What is a lambda expression in C++?
Lambda Expressions allow users to write an inline expression that can be used for short snippets of code in your C++ code which are not going to be reused and don’t require naming. The Lambda Expression construct is introduced in C++ 11 and further developed in the C++17 and C++20 standards. Because of its syntax and definition with [ ]
and ( )
and { }
, sometimes it is hard to understand or remember how it works. If you want to learn about Lambda Expressions, in this post below, we explain Lambda Expressions step by step,
How can I use std::sort with a Lambda Expression in C++?
Sometimes we require very specific sorting techniques when we sort members of data in range. In this case we can create our new modern sort
function, which is defined as a lambda expression.
Here is the Syntax for the std::sort
method with a lambda expression,
1 2 3 |
void sort( ExecutionPolicy&& policy, RandomIt first, RandomIt last, <Lambda Expression> ); // since C++17 |
Let’s assume that we have a lambda expression to perform the sort
comparison as below,
1 2 3 4 5 6 |
[](int x, int y) // lambda expression { return x > y; } |
If we combine both, here is a std::sort
example with a simple lambda expression
1 2 3 4 5 6 7 8 |
std::sort( vec.begin(), vec.end(), [](int x, int y) // lambda expression { return x > y; } ); |
Is there a full example of how to use std::sort with a Lambda Expression in C++?
Now, let’s see how we can sort a lot of vector elements with a lambda expression. 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 26 27 28 29 30 31 |
#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(std::execution::par, vec.begin(), vec.end()); std::sort( vec.begin(), vec.end(), [](int x, int y) // lambda expression { return x > y; } ); // 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++ 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