STL algorithm算法all_of的翻译及使用(2)

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

std::all_of

template <class InputIterator, class UnaryPredicate>
  bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Test condition on all elements in range

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:

其行为类似下面:

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



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

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


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

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







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值