【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 ...
随机推荐
- java 异步调用与多线程
异步与多线程的区别 一.异步和多线程有什么区别?其实,异步是目的,而多 线程是实现这个目的的方法.异步是说,A发起一个操作后(一般都是比较耗时的操作,如果不耗时的操作 就没有必要异步了),可以继续自顾 ...
- 【git】常用命令
// 下载Git项目git clone url // 显示当前git配置git config --list // 设置用户信息git config --global user.name "& ...
- BAT 命令 .bat
echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令 echo 表示显示此命令后的字符 echo off 表示在此语句后所有运行的命令都不显示命令行本 ...
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- 【转】C#具名参数和可选参数
源地址:https://www.cnblogs.com/similar/p/5006705.html 另:可选参数的一个陷阱 参考:https://www.cnblogs.com/still-wind ...
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
- Opencv3.0: undefined reference to cv::imread(cv::String const&, int)
使用opencv,编译出错: undefined reference to cv::imread(cv::String const&, int) 自opencv3.0之后,图像读取相关代码在i ...
- 【洛谷P3909】异或之积
题目大意:给定一个 N 个数字组成的序列,求 \[ \left(6 \times \sum_{i=1}^{N} \sum_{j=i+1}^{N} \sum_{k=j+1}^{N} A_{i} \tim ...
- 关于导入本地maven项目pom.xml出现missing artifact org....报错处理
一.导入本地maven项目步骤:
- rest-assured之如何指定请求数据(Specifying Request Data 包括请求参数、请求头、cookie等)
我们除了可以为一个请求指定请求参数之外,还可以指定请求头(header).cookies.请求体(body)以及请求内容类型(content-type)等,下面我们就来一一介绍一下: 一.请求HTTP ...