【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 = 0;
  while (first!=last) 
 {
    if (pred(*first))
         ++ret;
    ++first;
  }
  return ret;
}
复制代码

参数

  1. first, last: 输入迭代器指出首尾的位置,范围是[first, last),即包括第一个而不包括last。
  2. pred: 一元函数名字,接收范围内的一个元素作为参数,返回bool值。函数不得修改其参数。可以为函数指针或函数对象。

案例

案例1. pred为bool函数

代码

复制代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool IsOdd(int i)
{
    return ((i % 2) == 1);
}
int main()
{
    vector<int> vec;
    for(int i=0; i<10; ++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(7);  //函数对象比函数更灵活
    cout << count_if(vec.begin(), vec.end(), tmp) << endl;
}
复制代码

输出

4

复杂度

O(1)

参考

http://www.cplusplus.com/reference/algorithm/count_if/





本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3505927.html,如需转载请自行联系原作者

相关文章
|
存储 算法 C++
C++ STL算法系列1---count函数
一.count函数 algorithm头文件定义了一个count的函数,其功能类似于find。这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果。 编写程序读取一系列int型数据,并将它们存储到vector对象中,然后统计某个指定的值出现了多少次。
767 0
|
C++ 容器 算法
STL中的常用的vector,map,set,sort, list用法笔记 .
原帖地址:http://hi.baidu.com/yanfei_1/blog/item/a0a538331f5256f91a4cffba.html C++的标准模板库(Standard Template Library,简称STL)是一个容器和算法的类库。
1474 0
|
算法 搜索推荐 C++
STL之sort应用
STL之sort应用
【STL】vector的insert方法详解
#include   #include   using namespace std;   int main()   {       vector v(3);       v[0]=2;       v[1]=7;       v[2]=9;       v.insert(v.begin(),8);//在最前面插入新元素。
1081 0
|
容器 C++
std::map中函数用法集合
1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点  2 eg:  3   map mapTest;  4   typedef map::iterator ITER;  5   ITER iter=mapTest.
931 0
|
算法 C++ Java
STL中排序函数的用法(Qsort,Sort,Stable_sort,Partial_sort,List::sort)
都知道排序很重要,也学了各式各样的排序算法,冒泡、插入、归并等等,但其实在ACM比赛中,只要不是太慢的算法,都可以适用(除非某些题目卡时间卡的很死),这个时候,速度与技巧便成了关键,而在C++的标准库中,就已经定义好了一些排序函数,下面来一一介绍它们吧=7= Qsort 函数原型为void qs...
2529 0
|
C++
高效的使用stl::map和std::set
1、低效率的用法 // 先查找是否存在,如果不存在,则插入 if (map.find(X) == map::end()) // 需要find一次 {     map.insert(x); // 需要find一次 } // 下面这段代码是一个意思 if (0 == map.
824 0
|
8月前
|
存储 安全 算法
C++的内置数组和STL array、STL vector
C++的内置数组和STL array、STL vector

热门文章

最新文章