STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法

25 篇文章 1 订阅
8 篇文章 0 订阅

1 统计vector向量中指定元素出现的次数:count()算法

   利用STL通用算法统计vector向量中某个元素出现的次数:count()算法统计等于某个值的对象的个数

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm> //包含通用算法

using namespace std;

int_tmain(int argc, _TCHAR* argv[])

{

    vector<int> scores;

    scores.push_back(100);

    scores.push_back(80);

    scores.push_back(45);

    scores.push_back(75);

    scores.push_back(99);

    scores.push_back(100);

    int num = 0;

    num= count(scores.begin(),scores.end(),100);//统计100出现的次数

    cout<<"times: "<<num<<endl;

    return 0;

}

2 count_if():利用函数对象统计满足条件对象的个数

函数对象是一个至少带有一个operator()方法的类。

函数对象被约定为STL算法调用operator时返回true或false,它们根据这个来判定这个函数。

count_if()函数通过传递一个函数对象来做出比count()统计函数更加复杂的评估以确定一个对象是否应该被计数,即count_if()函数的功能等同于count()函数和一些条件语句。

例子:1

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;

conststring TC("0003");

classIsTB

{

public:

    IsTB(){cout<<"执行默认构造函数"<<endl;}

    bool operator()(string& saleRecord)

    {

       return (saleRecord.substr(0,4) == TC);

    }

};

int_tmain(int argc, _TCHAR* argv[])

{

    vector<string>saleRecordVec;

    saleRecordVec.push_back("0001 Soap");

    saleRecordVec.push_back("0002 Shampoo");

    saleRecordVec.push_back("0003 ToothBrush");

    saleRecordVec.push_back("0004 Toothpaste");

    saleRecordVec.push_back("0003 ToothBrush");

    int num = 0;

    num= count_if(saleRecordVec.begin(),saleRecordVec.end(),IsTB());

    cout<<"Times: "<<num<<endl;

    IsTBobjIsTB;

    //利用已有的对象

    num= count_if(saleRecordVec.begin(),saleRecordVec.end(),objIsTB);

    cout<<"Times: "<<num<<endl;

 

    return 0;

}

count_if()函数的第三个参数是类对象。

执行结果:

 

如果需要给函数对象传递更多的信息,那么可以通过定义一个非缺省的构造函数来初始化所需要的信息。

例子:2

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;

classIsTB

{

public:

    IsTB(string& str){ m_str =str;} //通过构造函数传递信息

    bool operator()(string&saleRecord)

    {

       return (saleRecord.substr(0,4) == m_str);

    }

private:

    stringm_str;

};

 

int_tmain(int argc, _TCHAR* argv[])

{

    vector<string>saleRecordVec;

    saleRecordVec.push_back("0001 Soap");

    saleRecordVec.push_back("0002 Shampoo");

    saleRecordVec.push_back("0003 ToothBrush");

    saleRecordVec.push_back("0004 Toothpaste");

    saleRecordVec.push_back("0003 ToothBrush");

    int num = 0;

    stringstr("0003");

    num= count_if(saleRecordVec.begin(),saleRecordVec.end(),IsTB(str));

    cout<<"Times: "<<num<<endl;

    IsTBobjIsTB(str);

    //利用已有的对象

    num= count_if(saleRecordVec.begin(),saleRecordVec.end(),objIsTB);

    cout<<"Times: "<<num<<endl;

 

    return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值