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-6741d6fc2249a317460649/]

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-6741d6fc224a3314445032/]

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

[crayon-6741d6fc224a6718342837/]

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-6741d6fc224a8218193129/]

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

[crayon-6741d6fc224aa502654548/]

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-6741d6fc224ac819214717/]

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-6741d6fc224ad215767566/]

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-6741d6fc224b1129481522/]

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

[crayon-6741d6fc224b3075950368/]

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-6741d6fc224b5328693715/]

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