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;
}

Edit
& Run

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:[email protected]

2014-9-1

于GDUT

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

时间: 2024-08-25 15:03:54

STL algorithm算法any_of译文及使用(3)的相关文章

STL algorithm算法集合

algorithm意为"演算法",是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模版函数. 编程语言 C++ 类    别 C++标准库 头文件 #include <algorithm> 命名空间 using namespace std; 目录 ? 不修改内容的序列操作: ? 修改内容的序列操作: ? 划分操作: ? 排序操作: ? 二分法查找操作: ? 集合操作: ? 堆操作: ? 最大/最小操作: 不修改内容的序列操作: adjacen

STL algorithm算法rmonve,rmove_if(47)

rmove原型: std::remove template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); 查找的得到第一个元素的位置,然后从此位置开始遍历容器,将后面的元素依次前移,跳过和value相同值的元素,也就是说,所有和value相同值的元素都会被覆盖,而其他的元素都会依次前移. 返回值是理论

STL algorithm算法generate和generate_n(22)

今后的stl算法部分就不贴cpluplus的原文了,简要的介绍为主. generate原型: std::generate template <class ForwardIterator, class Generator> void generate (ForwardIterator first, ForwardIterator last, Generator gen); 该函数是使用gen函数产生的值填充范围内元素的值. 其行为类似如下: 1 2 3 4 5 6 7 8 template &l

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得

STL algorithm算法minmax,minmax_element(36)

minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T&> minmax (const T& a, const T& b); custom (2) template <class T, class Compare> pair <const T&,const T&> minmax (const

STL algorithm算法merge(34)

merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custo

STL algorithm算法mov,move_backward(38)

move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result); 该函数是将指定范围内的元素移动到从result开始的位置. move之后,[first,last)范围内的元素去留的具体实现由编译器决定. result不能是在[first,last)

STL algorithm算法is_permutation(27)

is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class ForwardIterator2> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); predicate (2) template <class ForwardIter

STL algorithm算法is_sorted和is_sorted_until(28)

is_sort的原型: ::is_sorted default (1) template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> bool is_sorted (ForwardIterator first, ForwardIterato