//函数对象:
重载函数调用操作符的类, 其对象常称为函数对象(function object), 即它们是行为类
似函数的对象。 一个类对象, 表现出一个函数的特征, 就是通过“对象名+(参数列表)”的方式
使用一个类对象, 如果没有上下文, 完全可以把它看作一个函数对待。
这是通过重载类的 operator()来实现的。
“在标准库中, 函数对象被广泛地使用以获得弹性”, 标准库中的很多算法都可以使用函数
对象或者函数来作为自定的回调行为
#include"iostream"
using namespace std;
#include "vector"
#include"string"
#include"list"
#include"deque"
#include"algorithm"
#include"set"
#include"functional"
template<class T>
class myclass
{
public:
void operator()(T&t)
{
cout << "t="<<t << endl;
}
};
void print(int &t)
{
cout << "t=" << t << endl;
}
int main01()
{
int a = 10;
myclass<int> my1;
my1(a);//函数对象()的执行像一个函数//仿函数
print(a);
return 0;
}
//函数对象与普通函数的区别
//函数对象的好处
template <class T>
class A
{
public:
A()
{
sum = 0;
}
void operator()(int n)
{
sum += n;
//cout << sum << endl;
}
void print()
{
cout <<"sum=" <<sum << endl;
}
private:
T sum;
};
int main02()
{
A<int> a1;
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
vector<int>vec_1;
for (int i = 0; i < 10; i++)
{
vec_1.push_back(a[i]);
}
/*template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);
return (_STD move(_Func));
}
*/
//for_each形参传给实参是值传递,不是引用传递
for_each(vec_1.begin(), vec_1.end(), A<int>());
a1.print();//输出sum=0;
cout << "通过for_each 算法的返回值" << endl;
a1=for_each(vec_1.begin(), vec_1.end(), A<int>());
a1.print();//输出sum=55;
return 0;
}
///一元函数对象
template<class T>
class MyClass
{
public:
MyClass(const T div)
{
this->div = div;
}
bool operator()(T &t)
{
return(t%div==0);
}
private:
T div;
};
void main03()
{
int a2 = 3;
MyClass<int> my2(a2);
vector<int>vec_2;
for (int i = 10; i < 50; i++)
{
vec_2.push_back(i);
}
vector<int>::iterator it;
/*template<class _InIt,
class _Pr> inline
_InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
return (_Rechecked(_First,
_Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
*/ //find_if返回的是迭代器
//it=find_if(vec_2.begin(), vec_2.end(),MyClass<int>(a2));
it = find_if(vec_2.begin(), vec_2.end(), my2);
if (it == vec_2.end())
{
cout << "wwwwwwwwww" << endl;
}
else
{
cout << "第一个被3整除的数是:" << *it << endl;
}
}
///二元函数对象//
template<class T>
class add
{
public:
T operator()(T t1, T t2)
{
return t1 + t2;
}
private:
};
void main04()
{
vector<int>v1, v2, v3;
v1.push_back(1);
v1.push_back(1);
v1.push_back(1);
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
v3.resize(10);
/*
template<class _InIt1,
class _InIt2,
class _OutIt,
class _Fn2> inline
_OutIt transform(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{ // transform [_First1, _Last1) and [_First2, ...) with _Func
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_POINTER(_Dest);
_DEBUG_POINTER(_Func);
if (_First1 != _Last1)
return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Dest, _Func,
_Is_checked(_Dest)));
return (_Dest);
}
*/
//把运算结果迭代器的位置返回出来;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), add<int>());
for (vector<int>::iterator it1 = v3.begin(); it1 != v3.end(); it1++)
{
cout << *it1 << " ";
}
}
template<class T>
class paixu
{
public:
bool operator()(T &t1, T &t2)
{
return (t1>t2);
}
};
template<class T>
class bianli
{
public:
void operator()(T &t)
{
cout << t << " ";
}
};
template<class T>
class paixu1
{
public:
bool operator()(T&t1, T&t2)
{
return (t1<t2);
}
};
void main05()
{
vector<int>vec_3(10);
for (int i = 0; i < 10; i++)
{
int tem = rand() % 100;
vec_3[i] = tem;
}
for (vector<int>::iterator it3 = vec_3.begin(); it3 != vec_3.end(); it3++)
{
cout << *it3 << " ";
}
sort(vec_3.begin(),vec_3.end (),paixu<int>());
cout << endl;
/*for (vector<int>::iterator it3 = vec_3.begin(); it3 != vec_3.end(); it3++)
{
cout << *it3 << " ";
}*/
for_each(vec_3.begin(), vec_3.end(), bianli<int>());
sort(vec_3.begin(),vec_3.end(),paixu1<int>());
cout << endl;
for_each(vec_3.begin(), vec_3.end(), bianli<int>());
}
///二元函数对象在set集合中的应用//
struct funNocase
{
public:
bool operator()( const string &t1_1, const string &t2_1)
{
string t1;
t1.resize(t1_1.size());
transform(t1_1.begin(), t1_1.end(), t1.begin(), towlower);
string t2;
t2.resize(t2_1.size());
transform(t2_1.begin(), t2_1.end(), t2.begin(), towlower);
return (t1>t2);
}
private:
};
void main06()
{
//set< string >set_1; //区分大小写,默认从小到大排序 AAABBBCCCaaabbbccc
//set< string,greater<string> >set_1; //区分大小写,从大到小排序 cccbbbaaaCCCBBBAAA
set<string, funNocase>set_1; //不区分大小写,从大到小排序 cccbbbaaa
set_1.insert("aaa");
set_1.insert("bbb");
set_1.insert("ccc");
set_1.insert("AAA");
set_1.insert("BBB");
set_1.insert("CCC");
for (set<string>::iterator it4 = set_1.begin(); it4 != set_1.end(); it4++)
{
cout << *it4 << " ";
}
cout << endl;
}
int main()
{
//main01();
//main02();//函数对象做函数参数
//main03();//一元函数对象遍历
//main04();//二元函数对象相加
//main05();//二元函数对象排序
//main06();//二元函数在set中的应用
system("pause");
return 0;
}
//预定义函数对象
#include"iostream"
using namespace std;
#include"vector"
#include"deque"
#include"set"
#include"algorithm"
#include"functional"
#include"numeric"
/*预定义函数对象*/
//预定义函数对象分为几类:算数类函数对象、关系运算类函数对象、逻辑运算类对象。
//算 数 类:plus<T>、minus<T>、multiples<T>、divides<T>、modules<T>、negate<T>
// 输入参数、返回值为类型T;
//关系运算类:equal_to<T>、not_equal_to<T>、greater<T>、less<T>、greater_equal<T>、less_equal<T>
// 输入参数类型T、返回值为类型BOOL;
//逻辑运算类:logical_and<T>、logical_or<T>、logical_not<T>
// 输入参数类型T、返回值为类型BOOL;
/*算数类函数对象*/
void main01()
{
/基本数据类型//
//先建立一些实体对象,用对象履行函数功能
plus<int>plus_1;
cout << plus_1(4, 2) << endl; //6
minus<int>minus_1;
cout << minus_1(4, 2) << endl; //2
multiplies<int>multi_1;
cout << multi_1(4, 2) << endl; //8
divides<int>divide_1;
cout << divide_1(4, 2) << endl; //2
modulus<int>modul_1;
cout << modul_1(4, 2) << endl; //0
negate<int>negate_1;
cout << negate_1(4) << endl; //-4
//也可直接以函数对象的临时对象履行函数功能,function<T>是一个临时对象,调用operator运算符
/*
cout << plus<int>()(4, 2) << endl;
cout << minus<int>()(4, 2) << endl;
cout << multiplies<int>()(4, 2) << endl;
cout << divides<int>()(4, 2) << endl;
cout << modulus<int>()(4, 2) << endl;
cout << negate<int>()(4) << endl;
*/
}
/复杂数据类型//
//需要重载类中的各个operator运算符
//案例:求(1+2i)与(3+4i)的和
class Complex
{
public:
Complex()
{
this->real = 0;
this->virt = 0;
}
Complex(float real,float virt)
{
this->real = real;
this->virt = virt;
}
/*template<class _Ty = void>
struct equal_to
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator==
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator== to operands
return (_Left == _Right);
}
};
*/
bool operator == (const Complex&c1)const//此处注意const 需与equal_to<T>的定义一致
{
return ((real == c1.real) && (virt==c1.virt));
}
/*template<class _Ty = void>
struct not_equal_to
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator!=
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator!= to operands
return (_Left != _Right);
}
};*/
bool operator!=(const Complex &c1)const
{
return ((real != c1.real) && (virt != c1.virt));
}
/* // TEMPLATE STRUCT plus
template<class _Ty = void>
struct plus
: public binary_function<_Ty, _Ty, _Ty>
{ // functor for operator+
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator+ to operands
return (_Left + _Right);
}
};
*/
Complex operator + (const Complex &c1)const;//此处注意const 需与plus<T>的定义一致
friend ostream& operator<<(ostream&out, Complex&c1);
private:
float real;
float virt;
};
ostream& operator<<(ostream&out, Complex&c1)
{
out << c1.real << " + " << c1.virt << "i";
return out;
}
Complex Complex::operator + (const Complex &c1)const
{
Complex v;
v.real = real + c1.real;
v.virt = virt + c1.virt;
return v;
}
void main02()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
Complex c4=plus<Complex>()(c1, c2);
cout << "两个复数相加结果为: "<< c3 << endl; //需要重载<<;
cout << "两个复数相加结果为: "<< c4 << endl;
//此处两种结果相同,说明单独使用plus意义不大,需与stl算法结合才有意义;优势会很明显
vector<Complex>v;
v.push_back(c1);
v.push_back(c2);
v.push_back(c3);
v.push_back(c4);
Complex c;
/*template<class _InIt,
class _Ty,
class _Fn2> inline
_Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func)
{ // return sum of _Val and all in [_First, _Last), using _Func
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
return (_Accumulate(_Unchecked(_First), _Unchecked(_Last), _Val, _Func));
}
*/
Complex result = accumulate(v.begin(),v.end(),c,plus<Complex>());
cout << "所有复数和的结果为: "<< result << endl;
}
/*关系运算类函数对象*/
void main03()
{
//基本数据类型//
int a = 10, b = 100;
cout << "a= " << a <<" "<< "b= " << b << endl;
equal_to<int>equ1;
cout <<"a和b是否相等:"<< equ1(a, b) << endl; //0
not_equal_to<int>Noequ1;
cout << "a和b是否不相等:" << Noequ1(a, b) << endl; //1
less<int>less1;
cout << "a<b:" << less1(a, b) << endl; //1
greater<int>greater1;
cout << "a>b:" << greater1(a, b) << endl; //0
less_equal<int>lessequal1;
cout << "a<=b:" << lessequal1(a, b) << endl; //1
greater_equal<int>greaterequal1;
cout << "a>=b:" << greaterequal1(a, b) << endl; //0
}
//复杂数据类需要重载operator运算符//
void main04()
{
Complex c1(1, 2);
Complex c2(3, 4);
//先调用二元函数类中的operator==运算符函数,当数据类型复杂时,
//接着调用complex类中operator==函数,完成比较
cout << "c1和c2是否相等:" << equal_to<Complex>()(c1, c2) << endl; //0
cout << "c1和c2是否不相等:" << not_equal_to<Complex>()(c1, c2) << endl; //1
}
/*逻辑运算类*/
void main05()
{
cout << "(3<4)&&(5>9): " << logical_and<int>()(3 < 4, 5 > 9) << endl;//0
}
int main()
{
//main01();
//main02();
//main03();
//main04();
main05();
system("pause");
return 0;
}