函数对象 与 count_if()结合使用 例子
来源:互联网 发布:黄金td模拟软件 编辑:程序博客网 时间:2024/05/01 14:27
例程一、
/*
|| Using a function object to help count things
*/
#include <string>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
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);
NumberOfToothbrushes=count_if (SalesRecords.begin(), SalesRecords.end(),
IsAToothbrush() ); //第三个参数IsAToothbrush(),它是由它的构造函数临时构造的一个对象
#
cout << "There were "
<< NumberOfToothbrushes
<< " toothbrushes sold" << endl;
}
例程二、(与前面的例程一功能一致,改用函数来实现)
#include <string>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
const string ToothbrushCode("0003");
bool IsAToothbrush(string x)
{
return x.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);
NumberOfToothbrushes=count_if (SalesRecords.begin(), SalesRecords.end(),
IsAToothbrush );
#
cout << "There were "
<< NumberOfToothbrushes
<< " toothbrushes sold" << endl;
}
例程三、(更加复杂的函数对象,传递的信息更多,使用更灵活)
*//*
|| Using a more complex function object
*/
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
#
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);
NumberOfToothbrushes=count_if (SalesRecords.begin(), SalesRecords.end(),
IsAToothbrush(VariableToothbrushCode));
cout << "There were "
<< NumberOfToothbrushes
<< " toothbrushes matching code "
<< VariableToothbrushCode
<< " sold"
<< endl;
}
这个例子演示了如何向函数对象传递信息。你可以定义任意你想要的构造函数,你可以再函数对象中做任何你 想做的处理,都可以合法编译通过。
你可以看到函数对象真的扩展了基本记数算法
- 函数对象 与 count_if()结合使用 例子
- 函数对象与 for_each结合使用
- 【STL】标准库中count与count_if函数说明与例子
- AcceptEx函数与完成端口的结合使用例子
- AcceptEx函数与完成端口的结合使用例子
- C++函数对象count_if用法(转)
- 函数对象的构造与使用例子
- 使用netty结合Protostuff传输对象例子
- xdoclet与Ant结合使用-简单例子
- springMVC与jquery结合使用完整例子
- count_if函数详解
- stl count_if()函数
- count和count_if函数
- count_if(条件计数函数)
- count与count_if详解
- stl count_if使用
- popen 使用例子 c与sh,管道的结合使用
- AJAX与java servlet结合使用的小例子
- Unicode介绍
- java编程思想(第四版)_第2章 一切都是对象
- CSS高级教程 页面布局
- 在Spring中集成Hibernate事务
- j技术网站
- 函数对象 与 count_if()结合使用 例子
- 上班族饮食十大“夺命”恶习
- 应该如何合理的应用CSS整体布局声明
- Programming Problems 4
- JDK、JRE与JVM
- Spring事务的学习
- CSS优先权之深入剖析
- struts+hibernate项目debug总结
- Spring 事务简化配置