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] <

devx-admin

Share the Post: