Site icon Learn C++

How To Sort With The STL Compare Function Objects In C++

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.

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,

[crayon-6634dc74b002c101358863/]

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,

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;

[crayon-6634dc74b0033704295359/]

For example we can use it to sort a vector as below,

[crayon-6634dc74b0036581491637/]

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;

[crayon-6634dc74b0038291259644/]

For example we can use it to sort a vector as below,

[crayon-6634dc74b003a214795310/]

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;

[crayon-6634dc74b003b769763102/]

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.

[crayon-6634dc74b003d449236771/]

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,

[crayon-6634dc74b0044780778888/]

Let’s assume that we want to use std::greater function object, sort will be as below,

[crayon-6634dc74b0049260208280/]

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,

[crayon-6634dc74b004b940604488/]

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.

Exit mobile version