【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 ...
随机推荐
- MVC4 基于 Unity Ioc 框架的 ControllerFactory
首先引入Untiy框架. 可以在NuGet程序包 管理器中直接安装. 新建 继承DefaultControllerFactory 的UnityControllerFactory: 重写虚方法GetC ...
- tensorflow使用
近期在工作中使用tensorflow训练神经网络模型,使用方法简要记录. 环境配置
- Code First ef SQL Server 版本不支持数据类型“datetime2”
When calling DbContext.SaveChanges, I get a DbUpdateException:An unhandled exception of type 'System ...
- vector在一个循环里同时删除插入
vector<int> a; a.push_back(10); a.push_back(20); a.push_back(30); a.push_back(40); a.push_back ...
- TCP/IP学习笔记(2)-数据链路层
数据链路层有三个目的: 为IP模块发送和接收IP数据报. 为ARP模块发送ARP请求和接收ARP应答. 为RARP发送RARP请求和接收RARP应答 ip大家都听说过.至于ARP和RARP,ARP叫做 ...
- [Maven实战-许晓斌]-[第二章]-2.7-2.8 Mave安装的最优建议和安装小结
2.7
- EXPORT Man Information for Linux use COMMAND col
col命令 是一个标准输入文本过滤器,它从标注输入设备读取文本内容,并把内容显示到标注输出设备.在许多UNIX说明文件里,都有RLF控制字符.当我们运用shell特殊字符>和>>,把 ...
- NTP搭建指南
NTP搭建指南 前言: NTP是网络时间协议(Network Time Protocol),用于全球的标准时间(UTC)的校正. 一般NTP 服务有不同的层次:一层是源头NTP 服务器,一层服务器都设 ...
- classnofoundexception:org.springframework.ui.freemarker.FreeMarkerConfigurationFactory
这个是在整合freemarker的时候出现的问题,在添加依赖spring-context-support.jar之后就好了 但是我在搭建框架的时候出现的问题是明明添加了这个依赖,但是出现一个问题 出现 ...
- ubuntu15.04下安装jdk8
前几天手贱,删掉了ubuntu自带的java,最后安装时遇到了Picked up JAVA_TOOL_OPTIONS的问题,经过网上各种找,终于被我弄成功了.下面将经验下载下面供大家方便: jdk8的 ...