C++ Algorithm Predicates
Algorithm predicates are named functions that test one value and return true or false. Algorithms use a predicate to answer a question about a whole collection.
Three questions over one vector
Three algorithms test the same scores with named predicates. Each answer prints as a word on its own line.
#include <iostream>
#include <vector>
#include <algorithm>
bool isPassing(int score) {
return score >= 50;
}
bool isPerfect(int score) {
return score == 100;
}
bool isNegative(int score) {
return score < 0;
}
int main() {
std::vector<int> scores = {72, 85, 90, 68};
std::cout << std::boolalpha;
std::cout << "All passing: " << std::all_of(scores.begin(), scores.end(), isPassing) << "\n";
std::cout << "Any perfect: " << std::any_of(scores.begin(), scores.end(), isPerfect) << "\n";
std::cout << "None negative: " << std::none_of(scores.begin(), scores.end(), isNegative) << "\n";
return 0;
}The smallest and largest values
std::min_element and std::max_element return positions. Reading through each position gives the extreme value.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> readings = {14, 7, 21, 9, 18};
auto low = std::min_element(readings.begin(), readings.end());
auto high = std::max_element(readings.begin(), readings.end());
std::cout << "Lowest: " << *low << "\n";
std::cout << "Highest: " << *high << "\n";
return 0;
}Visiting each value
std::for_each passes each value to the named function in order. The function prints one value per line.
#include <iostream>
#include <vector>
#include <algorithm>
void printSteps(int steps) {
std::cout << steps << "\n";
}
int main() {
std::vector<int> steps = {4000, 6500, 5200};
std::for_each(steps.begin(), steps.end(), printSteps);
return 0;
}Example
#include <iostream>
#include <vector>
#include <algorithm>
bool reachedGoal(int steps) {
return steps >= 6000;
}
int main() {
std::vector<int> week = {8200, 4500, 9100, 6800, 7500, 10200, 5900};
std::cout << std::boolalpha;
std::cout << "All reached goal: " << std::all_of(week.begin(), week.end(), reachedGoal) << "\n";
std::cout << "Any reached goal: " << std::any_of(week.begin(), week.end(), reachedGoal) << "\n";
auto low = std::min_element(week.begin(), week.end());
auto high = std::max_element(week.begin(), week.end());
std::cout << "Lowest: " << *low << "\n";
std::cout << "Highest: " << *high << "\n";
return 0;
}Key points
- A predicate takes one value and returns true or false.
- std::all_of answers true when every value passes the predicate.
- std::any_of answers true when at least one value passes the predicate.
- std::none_of answers true when no value passes the predicate.
- std::min_element returns the position of the smallest value.
- std::max_element returns the position of the largest value.
- Reading through a returned position with * gives the value at that position.
In this example, the predicate tests whether one day reached 6000 steps. std::all_of gives false because some days fell short, and std::any_of gives true because some days reached the goal. The last two lines print the lowest and highest step counts of the week.
Note
std::for_each applies a named function to each value in order.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does std::all_of return when one value fails the predicate?