c++中STL怎样编写自己的函数对象
来源:互联网 发布:puppy linux 中文 编辑:程序博客网 时间:2024/05/01 08:16
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
template <class Arg, class Result>
class is_odd
{
public:
typedef Arg argument_type;
typedef Result res;
bool operator() ( const int& value)const {return (value%2==1); }
};
int main () {
int values[] = {1,2,3,4,5};
int cx;
cx = count_if ( values, values+5, not1(is_odd<int,bool>()) );
cout << "There are " << cx << " elements with even values.\n";
return 0;
#include <functional>
#include <algorithm>
using namespace std;
template <class Arg, class Result>
class is_odd
{
public:
typedef Arg argument_type;
typedef Result res;
bool operator() ( const int& value)const {return (value%2==1); }
};
int main () {
int values[] = {1,2,3,4,5};
int cx;
cx = count_if ( values, values+5, not1(is_odd<int,bool>()) );
cout << "There are " << cx << " elements with even values.\n";
return 0;
}
方法二:继承 函数对象
#include <iostream>#include <functional>using namespace std;struct IsOdd : public unary_function<int,bool> { bool operator() (int number) {return (number%2==1);}};int main () { IsOdd IsOdd_object; IsOdd::argument_type input; IsOdd::result_type result; cout << "Please enter a number: "; cin >> input; result = IsOdd_object (input); cout << "Number " << input << " is " << (result?"odd":"even") << ".\n"; return 0;}
- c++中STL怎样编写自己的函数对象
- 编写自己的php扩展函数(c)
- C语言编写自己的对数函数
- access中使用自己编写的函数
- 编写自己的对象
- 编写可适配的函数对象(Effective stl 条款40)
- 对STL中函数对象的认识
- 怎样编写能够被C调用的C++函数
- C语言自己编写的字符串操作的函数
- 怎样编写自己的DLL库文件?
- 怎样编写自己的C++头文件
- C++:编写自己的函数时要keep in mind
- 自己编写c语言实现的字符串操作函数
- R语言中如何编写自己的函数初步入门
- 编写自己的DEBUG_PRIN函数
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- javac编译成功,用java运行class文件出现“找不到或无法加载主类”
- 安卓超级省电技巧 9小时仅耗电2%
- DateUtil
- Tcl语言学习
- android系统学习笔记四
- c++中STL怎样编写自己的函数对象
- android系统移植
- 【面试笔试-c/c++】百度2011年校园招聘总结
- LIB和DLL的区别与使用
- 杨辉三角
- 失去和得到焦点OnActivate
- 我指间的刺青是对你的誓言
- 你不是富翁的7个原因
- String和StringBuffer的区别