STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
2013-09-20 20:54
531 查看
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;
}
相关文章推荐
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- STL 查找vector容器中的指定对象:find()与find_if()算法
- STL 查找vector容器中的指定对象:find()与find_if()算法
- 不会改变操作对象内容的STL算法(元素查找,序列查找,统计元素个数,比较区间,找最值)
- java统计字符串中指定元素出现次数方法
- 泛型算法——容器元素的排序算法(统计长度不小于6的单词个数)sort unique stable_sort count_if
- STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数
- stl 的find_if算法用来查找容器内的符合条件的元素
- 用vector向量容器装入10个整数,使用迭代器iterator和accumulate算法统计这10个元素的和
- STL_算法_元素计数(count、count_if)
- 寻找容器vector中出现最多的元素以及出现的次数
- 算法--微软面试:指定数字在数组中出现的次数
- 什么样的对象才能作为STL容器的元素
- c++在数组中查找元素并返回位置下标,统计某元素出现次数程序
- 随机生成长度为100的数组,数组元素为1到10,统计出现次数最多和最少的元素
- python 统计数组中元素出现次数并进行排序
- 如何统计一列数中每个元素出现的次数
- 关于STL的list,vector等用迭代器iterator,用erase删除元素出现的问题。
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- 使用STL通用算法find_if()在list中搜索对象