【STL】count_if
功能
返回满足条件的元素个数
模版
template <class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type //返回值
count_if (InputIterator first, InputIterator last, UnaryPredicate pred);
实现
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
typename iterator_traits<InputIterator>::difference_type ret = ;
while (first!=last)
{
if (pred(*first))
++ret;
++first;
}
return ret;
}
参数
- first, last: 输入迭代器指出首尾的位置,范围是[first, last),即包括第一个而不包括last。
- pred: 一元函数名字,接收范围内的一个元素作为参数,返回bool值。函数不得修改其参数。可以为函数指针或函数对象。
案例
案例1. pred为bool函数
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool IsOdd(int i)
{
return ((i % ) == );
}
int main()
{
vector<int> vec;
for(int i=; i<; ++i)
vec.push_back(i);
int mycount = count_if(vec.begin(), vec.end(), IsOdd);
cout << "My vector contains " << mycount << " odd values." << endl;
}
输出

案例2. pred为函数对象
代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class GT_cls
{
public:
GT_cls(int val) : bound(val) {}
bool operator()(const string &s)
{ return s.size() >= bound; }
private:
string::size_type bound;
};
int main()
{
vector<string> vec;
vec.push_back("hello1");
vec.push_back("hello12");
vec.push_back("hello123");
vec.push_back("hello1234");
vec.push_back("hello12345");
GT_cls tmp(); //函数对象比函数更灵活
cout << count_if(vec.begin(), vec.end(), tmp) << endl;
}
输出
4
复杂度
O(1)
参考
http://www.cplusplus.com/reference/algorithm/count_if/
【STL】count_if的更多相关文章
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- 蓝桥 ADV-233 算法提高 队列操作 【STL】
算法提高 队列操作 时间限制:1.0s 内存限制:256.0MB 问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. ...
- CF987A Infinity Gauntlet【STL】
[链接]:CF987A [分析]:运用map [代码]: #include <iostream> #include<queue> #include<string.h> ...
- 华农oj Problem B: Averyboy找密码【STL】
Problem B: Averyboy找密码 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 29 [Submit][Status] ...
- 【STL】帮你复习STL泛型算法 一
STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...
- 洛谷P1118 数字三角形【dfs】【STL】
题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...
- CF 1005A Tanya and Stairways 【STL】
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairw ...
随机推荐
- [LeetCode 题解]: LetterCombinations
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [Erlang08] 使用Erlang application有什么好处?
问题: 当我们把一个项目中所有的supervision tree通过一个简单的函数game: start(),会发现这个树结构特别复杂,只能有一个根节点,然后一直扩展. 那么有时,我们会需要:有些功能 ...
- openedx下载汉化包的一些操作
首先我是用debian9系统,然后装了xface桌面,感觉挺好用的,怎么确定路径是否和别人的一样了,我主要参照edx-platform这个文件夹的位置. 1. 参照大神的这个贴子https://www ...
- centos7 守护进程
ASP.NET Core应用程序发布linux在shell中运行是正常的.可一但shell关闭网站也就关闭了,所以要配置守护进程, 用的是Supervisor,本文主要记录配置的过程和过程遇到的问题 ...
- ItemContainerStyleSelector
ItemContainerStyleSelector是容器Style选择器 用法和ItemTemplateSelector差不多 同样也是也是继承类 StyleSelector,也是重写方法Selec ...
- 获取请求 header 中指定字段的值
private function getHeader($name) {//获取请求头中$name的值 $name = 'HTTP_' . $name; foreach ($_SERVER as $ke ...
- [ActionScript 3.0] 像素级碰撞检测
package { import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.Disp ...
- [ActionScript 3.0] AS利用ByteArray向PHP发送二进制数据生成图片
flash as3向php发送二进制数据,通过php保存成图片. AS端: package { import com.JPEGEncoder.JPGEncoder; import flash.disp ...
- rejected –non-fast-forward解决方法
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境.幸运的是,Eclipse 附带了一个标准的插件集,包括Java开 ...
- The server of Nginx(二)——Nginx基本功能配置
一.Nginx访问控制 (1)基于授权的访问控制 Nginx于Apache一样,可以实现基于用户授权的访问控制,当客户端要访问相应网站或者目录时要求输入用户名密码才能正常访问,配置步骤与Apache基 ...