函数对象
来源:互联网 发布:mac ppt 切换显示 编辑:程序博客网 时间:2024/06/05 12:58
函数对象\
函数对象有三种类型:关系、逻辑、运算。为使用函数对象,应包含<functional>头文件。
想了解C++中定义的函数对象,看这个链接:http://msdn.microsoft.com/zh-cn/library/vstudio/86ke4swd.aspx
1.关系类型的函数对象:
equal_to<T> 测试是否相等
not_equal_to<T> 测试是否不相等
greater<T> 测试是否大于
greater_equal<T>
less<T> 测试是否小于
less_equal<T>
2.逻辑类型的函数对象:
logical_end<T>
logical_not<T>
logical_or<T>
3.算术类型的函数对象:
divides<T>
modulus<T>
negate<T>
函数对象就是一个重载了操作符()的对象,例如C++中greater的定义:
template<class Type> struct greater : public binary_function <Type, Type, bool> { bool operator()( const Type& _Left, const Type& _Right ) const; };
equal_to对象:
template<class Type> struct equal_to : public binary_function<Type, Type, bool> { bool operator()( const Type& _Left, const Type& _Right ) const; };
logical_and对象:
template<class Type> struct logical_and : public binary_function<Type, Type, bool> { bool operator()( const Type& _Left, const Type& _Right ) const; };
一个使用equal_to的例子:
// functional_equal_to.cpp// compile with: /EHsc#include <vector>#include <functional>#include <algorithm>#include <iostream>using namespace std;int main( ){ vector <double> v1, v2, v3 ( 6 ); vector <double>::iterator Iter1, Iter2, Iter3; int i; for ( i = 0 ; i <= 5 ; i+=2 ) { v1.push_back( 2.0 *i ); v1.push_back( 2.0 * i + 1.0 ); } int j; for ( j = 0 ; j <= 5 ; j+=2 ) { v2.push_back( - 2.0 * j ); v2.push_back( 2.0 * j + 1.0 ); } cout << "The vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; cout << "The vector v2 = ( " ; for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) cout << *Iter2 << " "; cout << ")" << endl; // Testing for the element-wise equality between v1 & v2 transform ( v1.begin( ), v1.end( ), v2.begin( ), v3.begin ( ), equal_to<double>( ) ); cout << "The result of the element-wise equal_to comparison\n" << "between v1 & v2 is: ( " ; for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ ) cout << *Iter3 << " "; cout << ")" << endl;}
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 对象 函数
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象~
- 函数对象
- HTML学习笔记(十二)HTML 布局
- JavaScript中的类型
- 优化MySQL数据库性能的八种方法
- ARM指令中的内存访问指令的总结
- 我的笔经面经,给大家参考
- 函数对象
- Linux下内存分配与映射之一
- 告诉你,如何成就DBA职业生涯(1)
- Perl 学习笔记
- hdu 2102 A计划
- js调用php和php调用js的方法举例
- 什么叫做“假学习”?什么是“真学习”!!
- pydobc连接sql server数据库
- win7下mahout-0.7安装