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_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.
All Any Checks
all_any_checks.cpp
Replay: real traced execution (multi-file project)
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int threshold = 70;
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 = 60;
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 = 90;
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;
}
scores ← [72, 85, 91, 64], threshold ← 70, allMeet ← 0, anyMeet ← 1
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int threshold→ 70 = 70; //@threshold=60, 9089 bool allMeet→ 0 = std::all_of(scores[72, 85, 91, 64].begin(), scores.end(),10 [threshold](int score) {11 return score >= threshold;12 });13 bool anyMeet→ 1 = std::any_of(scores[72, 85, 91, 64].begin(), scores.end(),14 [threshold](int score) {15 return score >= threshold;16 });1718 std::cout << "threshold=" << threshold70 << std::endl;19 std::cout << "allMeet=" << allMeet0 << std::endl;20 std::cout << "anyMeet=" << anyMeet1 << std::endl;21 return 0;22}outputthreshold=70 allMeet=0 anyMeet=1
scores ← [72, 85, 91, 64], threshold ← 60, allMeet ← 1, anyMeet ← 1
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int threshold→ 60 = 60;89 bool allMeet→ 1 = std::all_of(scores[72, 85, 91, 64].begin(), scores.end(),10 [threshold](int score) {11 return score >= threshold;12 });13 bool anyMeet→ 1 = std::any_of(scores[72, 85, 91, 64].begin(), scores.end(),14 [threshold](int score) {15 return score >= threshold;16 });1718 std::cout << "threshold=" << threshold60 << std::endl;19 std::cout << "allMeet=" << allMeet1 << std::endl;20 std::cout << "anyMeet=" << anyMeet1 << std::endl;21 return 0;22}outputthreshold=60 allMeet=1 anyMeet=1
scores ← [72, 85, 91, 64], threshold ← 90, allMeet ← 0, anyMeet ← 1
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int threshold→ 90 = 90;89 bool allMeet→ 0 = std::all_of(scores[72, 85, 91, 64].begin(), scores.end(),10 [threshold](int score) {11 return score >= threshold;12 });13 bool anyMeet→ 1 = std::any_of(scores[72, 85, 91, 64].begin(), scores.end(),14 [threshold](int score) {15 return score >= threshold;16 });1718 std::cout << "threshold=" << threshold90 << std::endl;19 std::cout << "allMeet=" << allMeet0 << std::endl;20 std::cout << "anyMeet=" << anyMeet1 << std::endl;21 return 0;22}outputthreshold=90 allMeet=0 anyMeet=1