C++C++11C++14C++17Code SnippetLearn C++Syntax

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.

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,

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

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

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.

Note that, in C++ std::array is on the stack, in other words it has less limits than vectors.

How To Use std::sort With A Lambda Expression In C++ - the C++ Builder Logo

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.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?