algorithm
[C++ 11] std::all_of
原型:
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
功能: 指定上下界内是否都符合某条件
用例:
#include<iostream>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int numbers[] = { 1, 3, 5, 7, 9, 11 };
if (all_of(begin(numbers), end(numbers), [](int i)
{
return i & 1;
})) {
cout << "所有的数都是奇数" << endl;
} else {
cout << "部分数字不是奇数" << endl;
}
return 0;
}
[C++ 11] std::any_of
原型:
template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
功能: 测试指定上下界内是否有元素满足条件
用例:
#include<iostream>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int numbers[] = { 1, 3, 5, 7, 9, 11, 6 };
if (any_of(begin(numbers), end(numbers), [](int i)
{
return i & 1;
})) {
cout << "数组中存在偶数" << endl;
} else {
cout << "数组中不存在偶数" << endl;
}
return 0;
}
[C++ 11] std::none_of
原型:
template <class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
功能: 测试指定上下界内是否均不符合某一条件
用例:
#include<iostream>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int numbers[] = { 1, 3, 5, 7, 9, 11, 6 };
if (none_of(begin(numbers), end(numbers), [](int i)
{
return i & 1;
})) {