C++C++14C++17Learn 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,

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,

  • std::execution::seq
  • std::execution::par
  • std::execution::par_unseq

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;

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

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;

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

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;

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.

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,

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

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,

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.

How To Sort With The STL Compare Function Objects 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond