一、all_of
头文件algorithm
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
测试范围内所有元素的条件
如果pred对范围[first,last]中的所有元素返回true或者范围为空,则返回true,否则返回false。
此函数模板的行为等效于:
template<class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (!pred(*first)) return false;
++first;
}
return true;
}
参数
- first,last
将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
如果pred对范围内的所有元素返回true或者范围为空,则为true,否则为false。
// all_of example
#include <iostream> // std::cout
#include <algorithm> // std::all_of
#include <array> // std::array
int main () {
std::array<int,8> foo = {3,5,7,11,13,17,19,23};
if ( std::all_of(foo.begin(), foo.end(), [](int i) {
return i%2;
}) )
std::cout << "All the elements are odd numbers.\n";
return 0;
}
例外
如果pred或者迭代器上的操作抛出,则抛出。请注意,无效参数会导致未定义的行为。
二、any_of
头文件algorithm
template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
测试范围内的任何元素是否满足条件
如果pred对范围[first,last]中的任何元素返回true,则返回true,否则返回false。
如果[first,last)是空范围,则该函数返回false。
此函数模板的行为等效于:
template<class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return true;
++first;
}
return false;
}
参数
- first,last
将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
如果pred对范围[first,last]中的任何元素返回true,则返回true,否则返回false。如果[first,last)是空范围,则该函数返回false。
// any_of example
#include <iostream> // std::cout
#include <algorithm> // std::any_of
#include <array> // std::array
int main () {
std::array<int,7> foo = {0,1,-1,3,-3,5,-5};
if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are negative elements in the range.\n";
return 0;
}
三、none_of
头文件algorithm
template <class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
测试没有元素满足条件
如果pred对范围[first,last]中的所有元素返回false或者范围为空,则返回true,否则返回false。
此函数模板的行为等效于:
template<class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return false;
++first;
}
return true;
}
参数
- first,last
将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
如果pred对范围[first,last]中的所有元素返回false或者范围为空,则为true,否则为false。
// none_of example
#include <iostream> // std::cout
#include <algorithm> // std::none_of
#include <array> // std::array
int main () {
std::array<int,8> foo = {1,2,4,8,16,32,64,128};
if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are no negative elements in the range.\n";
return 0;
}