【STL】count_if

本文详细介绍了C++标准库中的count_if函数,该函数用于统计满足特定条件的元素数量。提供了两种使用示例,一种是使用bool类型的函数作为谓词,另一种是使用函数对象。同时展示了如何定义函数对象来提高代码灵活性。

功能

返回满足条件的元素个数

模版

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,如需转载请自行联系原作者

std::count_ifSTL 中的一个算法,用于对容器中的元素进行条件统计。它的函数原型如下: ```c++ template <class InputIterator, class Predicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, Predicate pred); ``` 其中,first 和 last 分别表示容器中要进行统计的元素范围,pred 是一个谓词函数,用于对容器中的每个元素进行判断。count_if 函数会遍历容器中的每个元素,对每个元素都调用谓词函数 pred 进行判断,如果返回值为 true,则该元素被认为是符合条件的,计入统计结果。 count_if 函数返回符合条件的元素个数,其类型为 iterator_traits<InputIterator>::difference_type,表示两个迭代器之间的距离,通常是一个整型数。 下面是一个简单的例子,用于说明 std::count_if 算法的用法: ```c++ #include <iostream> #include <vector> #include <algorithm> bool isOdd(int num) { return num % 2 == 1; } int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // 使用 std::count_if 算法和 isOdd 谓词函数,统计 vec 中奇数的个数 int count = std::count_if(vec.begin(), vec.end(), isOdd); std::cout << "Count: " << count << std::endl; return 0; } ``` 在这个例子中,我们定义了一个谓词函数 isOdd,用于判断一个数是否为奇数。然后,我们使用 std::count_if 算法和 isOdd 谓词函数来统计 vec 容器中奇数的个数。在调用 std::count_if 算法时,需要传入谓词函数 isOdd 作为第三个参数,表示对容器中的每个元素都要调用该函数进行判断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值