Site icon Learn C++

How To Use std::sort With A Lambda Expression In C++

How To Use stdsort With A Lambda Expression In C++

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.

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.

[crayon-6638c2709e2ab051644593/]

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:

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,

[crayon-6638c2709e2b2161363035/]

Let’s assume that we have a lambda expression to perform the sort comparison as below,

[crayon-6638c2709e2b5703483739/]

If we combine both, here is a std::sort example with a simple lambda expression

[crayon-6638c2709e2b7441736340/]

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.

[crayon-6638c2709e2b9624934120/]

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.

Exit mobile version