孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

count是一种非可变序列算法,其功能是在序列中查找等于某个给定值的元素的个数。示例如下:

// Illustrating the generic count algorithm
#include <iostream>
#include
<cassert>
#include
<algorithm>
#include
<functional>
using namespace std;

int main()
{
cout
<< "Illustrating the generic count algorithm." << endl;
int a[] = {0, 0, 0, 1, 1, 1, 2, 2, 2};

// Count the number of values in the array a
// that are equal to 1:
int final_count = count(&a[0], &a[9], 1);

assert (final_count
== 3);

// Determine the number of array elements that are not
// equal to 1:
final_count = count_if(&a[0], &a[9],
bind2nd(not_equal_to
<int>(), 1));

// There are 6 elements not equal to 1.
assert (final_count == 6);
cout
<< " --- Ok." << endl;
return 0;
}
posted on 2011-06-03 21:22  孤独的猫  阅读(4367)  评论(0编辑  收藏  举报