STL algorithm算法any_of译文及使用(3)

原文地址:http://www.cplusplus.com/reference/algorithm/any_of/
function template
<algorithm>

std::any_of

template <class InputIterator, class UnaryPredicate>
  bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Test if any element in range fulfills condition
Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.

如果范围内任一元素使pred返回true,则返回true,否则返回false.

例子:

#include <iostream>
#include <algorithm>
using namespace std;
bool isGreat(int i){
	if(i>=5){
		cout<<i<<">=5"<<" ,match!"<<endl;
		return true;
	}
	else{
		cout<<i<<"<5"<<" ,mismatch!"<<endl;
		return false;
	}
}
int main()
{
	vector<int> vi{0,1,2,3,4,5,6};
	if(any_of(vi.begin(),vi.end(),isGreat))
		cout<<"Yes,some elements in vi  >=5 "<<endl;
	else
		cout<<"no,all elements in vi didn't >=1 "<<endl;


	cout<<endl;
	int ar[5]={10,20,30,40,50};
	if(any_of(ar,ar+5,[](int i){return i%10;}))
		cout<<"some in ar can %10!=0"<<endl;
	else
		cout<<"all %10=0!"<<endl;


}
运行截图:



If [first,last) is an empty range, the function returns false.

如果范围为空,则返回false.(因为没有任一匹配)

例子:

#include <iostream>
#include <algorithm>
using namespace std;
bool isGreat(int i){
	if(i>=5){
		cout<<i<<">=5"<<" ,match!"<<endl;
		return true;
	}
	else{
		cout<<i<<"<5"<<" ,mismatch!"<<endl;
		return false;
	}
}
int main()
{
	vector<int> vi{0,1,2,3,4,5,6};
	if(any_of(vi.begin(),vi.begin(),isGreat))
		cout<<"emptiy=true "<<endl;
	else
		cout<<"emptiy=false "<<endl;



}



The behavior of this function template is equivalent to:

行为类似以下:

1
2
3
4
5
6
7
8
9
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;
}



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之间的所有元素,包括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 any of the elements in the range [first,last), and false otherwise.

如果任一的元素对pred的返回值是true,那么返回true,否则返回false.


If [first,last) is an empty range, the function returns false.

如果范围为空,则返回false.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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;
}


Output:
There are negative elements in the range.

Complexity

Up to linear in the distance between first and last: Calls pred for each element until a match 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或者操作迭代器会抛出异常就会抛出异常。

无效的参数导致未定义的行为。



——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-1

于GDUT

——————————————————————————————————————————————————————————————————




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值