devxlogo

Using Predicate Objects to Alter Computation of a Generic Algorithm

Using Predicate Objects to Alter Computation of a Generic Algorithm

A predicate is an expression that returns either true or false. Similarly, a function object that returns a Boolean value is a predicate object. The Standard Template Library has several predicate objects that can be used to alter the computation of a generic algorithm. For example, you can alter the computation of sort() by its predicate:

 #include  //definitions of STL predicates#include  //definition of sort #include #include using namespace std;int main(){  vector  vi;  vi.push_back(9);  vi.push_back(5);  vi.push_back(10);  sort(vi.begin(), vi.end(), greater () );  // descending order  cout<< vi[0] << '	' << vi[1] << '	' << vi[2] < () );   // now in ascending order  cout<< vi[0] << '	' << vi[1] << '	' << vi[2] <
See also  5 Tips to Manage a Remote Workforce
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist