stl count_if()函数

22 篇文章 0 订阅

6.用STL的通用算法count_if()来统计list中的元素个数

 

    count_if()是count()的一个更有趣的版本。他采用了STL的一个新组件,函数对象。count_if() 带一个函数对象的参数。函数对象是一个至少带有一个operator()方法的类。有些STL算法作为参数接收函数对象并调用这个函数对象的operator()方法。

    函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会说的更清楚些。

    count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被记数。

 

    在这个例子里我们将数一数牙刷的销售数量。我们将提交包含四个字符的销售码和产品说明的销售记录。

 

/* || Using a function object to help count things */

#include <string>

#include <list>

#include <algorithm>

 

const string ToothbrushCode("0003");

 

class IsAToothbrush

{

public:

    bool operator() ( string& SalesRecord )

    {

        return SalesRecord.substr(0,4)==ToothbrushCode;

    }

};

 

int main (void)

{

    list<string> SalesRecords;

    SalesRecords.push_back("0001 Soap");

    SalesRecords.push_back("0002 Shampoo");

    SalesRecords.push_back("0003 Toothbrush");

    SalesRecords.push_back("0004 Toothpaste");

    SalesRecords.push_back("0003 Toothbrush");

    int NumberOfToothbrushes(0);

    count_if (SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(), NumberOfToothbrushes);

    cout << "There were " << NumberOfToothbrushes << " toothbrushes sold" << endl;

}

 

这是这个程序的输出:

There were 2 toothbrushes sold

 

这个程序是这样工作的:定义一个函数对象类IsAToothbrush,这个类的对象能判断出卖出的是否是牙刷 。如果这个记录是卖出牙刷的记录的话,函数调用operator()返回一个true,否则返回false。

 

count_if()算法由第一和第二两个iterator参数指出的范围来处理容器对象。它将对每个 IsAToothbrush()返回true的容器中的对象增加NumberOfToothbrushes的值。

 

最后的结果是NumberOfToothbrushes这个变量保存了产品代码域为"0003"的记录的个数,也就是牙刷的个数。

 

注意count_if()的第三个参数IsAToothbrush(),它是由它的构造函数临时构造的一个对象。你可以把IsAToothbrush类的一个临时对象 传递给count_if()函数。count_if()将对该容器的每个对象调用这个函数。

 

 

--------------------------------------------------------------------------------

 

7 使用count_if()的一个更加复杂的函数对象。

 

使用count_if()的一个更加复杂的函数对象。

    我们可以更进一步的研究一下函数对象。假设我们需要传递更多的信息给一个函数对象。我们不能通过调用operator来作到这点,因为必须定义为一个list的中的对象的类型。 然而我们通过为IsAToothbrush指出一个非缺省的构造函数就可以用任何我们所需要的信息来初始化它了。 例如,我们可能需要每个牙刷有一个不定的代码。我们可以把这个信息加到下面的函数对象中:

 

/*

|| Using a more complex function object

*/

#include <iostream.h>

#include <string>

#include <list>

#include <algorithm>

 

class IsAToothbrush

{

public:

    IsAToothbrush(string& InToothbrushCode) : ToothbrushCode(InToothbrushCode) {}

    bool operator() (string& SalesRecord)

    {

        return SalesRecord.substr(0,4)==ToothbrushCode;

    }

private:

    string ToothbrushCode;

};

 

int main (void)

{

    list<string> SalesRecords;

 

    SalesRecords.push_back("0001 Soap");

    SalesRecords.push_back("0002 Shampoo");

    SalesRecords.push_back("0003 Toothbrush");

    SalesRecords.push_back("0004 Toothpaste");

    SalesRecords.push_back("0003 Toothbrush");

 

    string VariableToothbrushCode("0003");

 

    int NumberOfToothbrushes(0);

    count_if (SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(VariableToothbrushCode), NumberOfToothbrushes);

    cout << "There were  "

         << NumberOfToothbrushes

         << " toothbrushes matching code "

         << VariableToothbrushCode

         << " sold"

         << endl;

}

 

程序的输出是:

There were 2 toothbrushes matching code 0003 sold

 

这个例子演示了如何向函数对象传递信息。你可以定义任意你想要的构造函数,你可以再函数对象中做任何你 想做的处理,都可以合法编译通过。

 

你可以看到函数对象真的扩展了基本记数算法。

 

本文转载自百度文库。作者如下。其中下面的count, count_if等函数的使用有些陈旧,如在编译时遇到问题,请百度。

标准模板库(STL)介绍

 

作者:Scott Field

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::count_if 是 STL 中的一个算法,用于对容器中的元素进行条件统计。它的函数原型如下: ```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、付费专栏及课程。

余额充值