Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Advanced
Feb 4, 1999



Application Security Testing: An Integral Part of DevOps

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 <functional> //definitions of STL predicates
#include <algorithm> //definition of sort 
#include <vector>
#include <iostream>
using namespace std;

int main()
  vector <int> vi;
  sort(vi.begin(), vi.end(), greater<int> () );  // descending order
  cout<< vi[0] << '\t' << vi[1] << '\t' << vi[2] <<endl;   // output: 10  9  5
  sort(vi.begin(), vi.end(), less<int> () );   // now in ascending order
  cout<< vi[0] << '\t' << vi[1] << '\t' << vi[2] <<endl;   // output: 5  9  10
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date