Iterator and Algorithm Patterns
All Any Checks
std::all_of and std::any_of summarize whether every value or at least one value satisfies a predicate.
All Any Checks
all_any_checks.cpp
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int threshold = ;
bool allMeet = std::all_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
bool anyMeet = std::any_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
std::cout << "threshold=" << threshold << std::endl;
std::cout << "allMeet=" << allMeet << std::endl;
std::cout << "anyMeet=" << anyMeet << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int threshold = ;
bool allMeet = std::all_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
bool anyMeet = std::any_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
std::cout << "threshold=" << threshold << std::endl;
std::cout << "allMeet=" << allMeet << std::endl;
std::cout << "anyMeet=" << anyMeet << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int threshold = ;
bool allMeet = std::all_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
bool anyMeet = std::any_of(scores.begin(), scores.end(),
[threshold](int score) {
return score >= threshold;
});
std::cout << "threshold=" << threshold << std::endl;
std::cout << "allMeet=" << allMeet << std::endl;
std::cout << "anyMeet=" << anyMeet << std::endl;
return 0;
}
all_of
`std::all_of` is true only when every element satisfies the predicate.
any_of
`std::any_of` is true when at least one element satisfies the predicate.