all_of用于判断指定范围内的元素是否都满足条件;
any_of用于判断指定范围内的元素是否至少有一个满足条件;
none_of用于判断指定范围内的元素是否都不满足条件。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v{ 1,9,2,8,3,7,4,6,5 };
int value = 10;
cout << (all_of(v.begin(), v.end(), [value](int i) {return i < value; }) ? "all " : "not all ") << "elements less than " << value << endl;
value = 6;
cout << (any_of(v.begin(), v.end(), [value](int i) {return i > value; }) ? "any " : "none ") << "of elements greater than " << value << endl;
value = 10;
cout << (none_of(v.begin(), v.end(), [value](int i) {return i > value; }) ? "none " : "not all ") << "of elements greater than " << value << endl;
return 0;
}
输出结果:
all elements less than 10
any of elements greater than 6
none of elements greater than 10