非修改性序列算法之count和count_if
来源:互联网 发布:淘宝网折800女鞋 编辑:程序博客网 时间:2024/06/05 16:04
count主要用来统计容器内元素个数,其格式有两种:
第一种,第三个参数为元素值
template<class _InIt, class _Ty> inline typename iterator_traits<_InIt>::difference_type count(_InIt _First, _InIt _Last, const _Ty& _Val)
第二种,第三个参数为条件函数:
template<class _InIt, class _Pr> inline typename iterator_traits<_InIt>::difference_type count_if(_InIt _First, _InIt _Last, _Pr _Pred)
程序实例:
#include<iostream>#include<functional>#include<algorithm>#include<vector>template<typename T>int PushNum(T &vec, int first, int last) { int ret = 0; if (first > last) { ret = -1; cout << "function PushNum first > last error:" << ret << endl; } while (first <= last) { vec.push_back(first); first++; } return ret;}template<typename T>void print(T &ele) { cout << ele << " ";}bool isEven(int &ele) { return ele % 2 == 0;}using namespace std;int main(){ vector<int> vec; PushNum(vec, 1, 10); for_each(vec.begin(), vec.end(), print<int>); cout << endl; int count1 = count(vec.begin(), vec.end(), 5); int count2 = count_if(vec.begin(), vec.end(), isEven); int count3 = count_if(vec.begin(), vec.end(), bind2nd(greater<int>(), 2)); cout << count1 << endl; cout << count2 << endl; cout << count3 << endl; system("pause"); return 0;}
输出结果:
第一个count是求元素值等于5的个数,第二个count是求满足偶数的个数,第三个count是求大于2的个数。
0 0
- 非修改性序列算法之count和count_if
- 非变异算法之计数(count, count_if)
- STL算法之count. count_if
- STL之 count 和 count_if
- 非修改性序列算法之min_element和max_element
- 非修改性序列算法之for_each
- 非修改性序列算法之mismatch
- C++ count和count_if
- count和count_if函数
- count_if 和count
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL之非修改性序列算法1
- STL之非修改性序列算法2
- STL之非修改性序列算法3
- count,count_if
- count count_if
- 泛型算法系列9:count()&&count_if()
- C语言编译常见错误
- 【编程题】股票涨价的问题
- 51nod oj 1086 背包问题 V2 【多重背包问题】
- MySQL安装常见问题
- Linux Deploy; apt upgrade 出错 dpkg cannot get security labeling handle: No such file or directory 解决
- 非修改性序列算法之count和count_if
- 数据库三大范式
- 【转】Java SwingWorker(一)
- 设计模式-命令模式
- 2016中国大学生程序设计竞赛 1004 Danganronpa
- linux 信号机制分析
- 剑指Offer面试题3:二维数组中的查找Java版解法
- Java原子变量类
- 朋友成员(距离)