std::all_of
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Returns true
if pred returns true
for all the elements in the range [first,last)
or if the range is empty, and false
otherwise.
如果对于范围[first,last)内每个元素,一元断言都为真,或者范围为空,那么返回true,否则返回false.
The behavior of this function template is equivalent to:
其行为类似下面:
|
|
Parameters
-
first, last
Input iterators to the initial
and final positions in a sequence. The range used is[first,last)
, which contains all theelements between first and last, including the element pointed by first but not the
element pointed by last.标示序列范围的输入迭代器。包括first指向的元素,但不包括last.
- pred
- Unary function that accepts an element in the range as argument and returns a value convertible to
bool
. The value returned indicates whether the element fulfills the condition checked
by this function.
The function shall not modify its argument.
This can either be a function pointer or a function object。
-
一个接受一个元素类型参数并返回一个bool值的一元函数。
-
可以是一个指针或者函数对象。
Return value
true
if pred returns true
for all the elements in the range or if the range is empty, and false
otherwise.
如果所有的元素对pred的返回值都是true,那么返回true,否则返回false.
例子:
#include <iostream> #include <algorithm> using namespace std; bool isGreatOne(int i){ if(i>=1) return true; else return false; } int main() { vector<int> vi{0,1,2,3,4,5,6}; if(all_of(vi.begin(),vi.end(),isGreatOne)) cout<<"Yes,all elements in vi >1 "<<endl; else cout<<"no,some of elements in vi didn't >=1 "<<endl; cout<<endl; int ar[5]={10,20,30,40,50}; if(all_of(ar,ar+5,[](int i){return i%10;})) cout<<"all in ar can %10!=0"<<endl; else cout<<"some %10=0!"<<endl; }
运行截图:
Example
|
|
Output:
All the elements are odd numbers.
|
Complexity
Up to linear in the distance between first and last: Calls pred for
each element until a mismatch is found.
和first,last之间的距离线性相关,对每个元素调用pred直到失配。
Data races
Some (or all) of the objects in the range [first,last)
are accessed (once at most).
最少一个元素被访问。
Exceptions
Throws if either pred or an operation on an iterator throws.
Note that invalid parameters cause undefined behavior.
如果pred或者操作迭代器会抛出异常就会抛出异常。
无效的参数导致未定义的行为。
例子:
<span style="color:#993399;">#include <iostream> #include <algorithm> using namespace std; bool isGreatOne(int i){ if(i>=1) return true; else return false; } int main() { vector<int> vi{1,2,3,4,5,6}; vector<int> v2{10,20}; if(all_of(vi.begin(),v2.end(),isGreatOne)) cout<<"Yes,all elements in vi >1 "<<endl; else cout<<"no,some of elements in vi didn't >=1 "<<endl; }</span>
注意:
if(all_of(vi.begin(),v2.end(),isGreatOne))
运行截图:
使用gdb调试会发现,当i到了vi.end()之后,还会继续向后访问,知道出现一个失配的值。
一般这个函数可以用来判断某一范围内的所有元素是否符合某种一元断言。
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-1
于GDUT
——————————————————————————————————————————————————————————————————