【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 ...
随机推荐
- 求帮忙解决封装jquery图片滚动问题
今天用jquery封装了点击图片滚动,但是发现在屏幕自适应时,图片停在的位置会随着屏幕大小而错位(我引入了pocketgrid.css响应式文件,但没办法去那边修改onsize事件...),求大神.. ...
- 慎用uniapp开发商业级应用
官方的社区反馈问题只给解决简单的前端问题,涉及到IDE的问题长期没人回复没人认领 官方公布的各渠道联系方式都得不到回复,先后出现了两个无法解决的问题 第一个问题(现在你都可以去他们社区搜索,没人回复没 ...
- C++构造和析构的顺序
C++构造函数和析构函数的顺序 #include <iostream> using namespace std; class CA {public: CA() { cout << ...
- nowcoder(牛客网)提高组模拟赛第四场 解题报告
T1 动态点分治 就是模拟..... 但是没有过!! 看了题解之后发现.... 坑点:有可能 \(x<=r\),但是
- 如何获得Android设备名称(ADB命令详细介绍)
豌豆荚.360手机管家等软件可以获取android设备名称,显示在界面上,如下图: 我们自己如何来获取设备名称 呢?答案如下: 在命令行中输入“adb shell”进入shell之后,再输入“cat ...
- 【maven】---聚合和继承
前言 自从我知道写maven实战这本书的作者长得随心所欲后,我再拿起这本书真心的不想看前言了.下面分享一下maven中的所谓的聚合和继承. 内容 下文中的子本指的是:多个maven项目. 父本指的是: ...
- maven+eclipse+ssm 环境搭建和启动
该类工程环境搭建和启动方法 ------------------------------------------------------------------------------- 配置 jdk ...
- CF223C【Partial Sums】(组合数学+乱搞)
题面 传送门 题解 orz zzk 考虑这东西的组合意义 (图片来自zzk) \(a_i\)这个元素对\(k\)阶前缀和的第\(j\)个元素\(s_{k,j}\)的贡献就等于从\((0,i)\)走到\ ...
- WordPress翻译更新失败解决方法
编辑php的配置文件:php.ini,搜索并找到disable_functions: 删除disable_functions后面的scandir字符串,保存php.ini: 重载或重启php-fpm服 ...
- 【算法】C++用链表实现一个箱子排序附源代码详解
01 箱子排序 1.1 什么是分配排序? 分配排序的基本思想:排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O ...