【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 ...
随机推荐
- IIS应用程序池自动回收作业
vb: appPoolName = WScript.Arguments() Set oWebAdmin = GetObject("winmgmts:root\WebAdministratio ...
- 【海量干货】89页PPT详解微信O2O行业解决方案
根据腾讯大讲堂提供的信息,整理成了PPT,下载地址: http://yunpan.cn/cZAbTnJXnMymd 访问密码 f36d
- Android 打开URL中的网页和拨打电话、发送短信功能
拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...
- eclipse 中文或法文等语言注释错误解决办法 Some characters cannot be mapped using "GBK" character encoding
这个问题会造成 无法修改包名.解决办法: Window->Preferences->Content Types->Text->Java Source File Default ...
- Hbase的安装与测试
实验环境 虚拟机伪分布式 Ubuntu 17.10 JDK 1.8 Hadoop 2.7.6 Hbase 1.3.3 ①安装和配置HBase. 首先从官网http://archive.apache.o ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study
死于update的一个long long写成int了 真的不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/ ...
- [转] 迁移 VMware 虚拟机到 KVM
迁移 VMware 虚拟机到 KVM 如何迁移 VMware 上的 Windows 及 Linux 虚拟机到基于 KVM 的虚拟机 目前 x86 平台上最流行的虚拟化软件是 VMware 公司的系列产 ...
- 8,Phaser__并发且多阶段任务
使用场景 考选武状元 10 个 武生 参加考试 ,第一个关 靠耐力, 坚持最久的5个人进入第二关, 第二关考 力气,力气最大的 3个人进入第二关,第三关考兵法,兵法最好的当选武状元
- 用JS实现汉字转拼音
<!DOCTYPE HTML> <html> <head> <title>用JS实现汉字转拼音</title> <meta chars ...