Originally Posted by
843
What's a predicate?
A function, or function object, that returns a bool depending on some condition typically related to some property of one or more objects, e.g., if an object is "less than" another object.
Originally Posted by
843
Could you please give me a quick example?
Yes:
Code:
#include <algorithm>
#include <iostream>
struct Player
{
int points;
};
// Predicate to compare Player objects by points.
bool compareByPoints(const Player& x, const Player& y)
{
return x.points < y.points;
}
int main()
{
using namespace std;
Player players[4] = {{2}, {4}, {1}, {3}};
cout << max_element(players, players + 4, compareByPoints)->points << endl;
}