函数对象
概念
重载函数调用运算符的类实例化的对象
,
就叫函数对象
.
又名仿函数
函数对象和
()
触发重载函数调用运算符的执行。
作用
:
为算法提供策略。
示例
class MyPrint
{
public:
void operator()(char *s)
{
cout<<s<<endl;
}
};
void test01()
{
MyPrint ob1;
ob1("hello world");
MyPrint()("hello world");
}
谓词
概念
返回值为
bool
类型的普通函数 或 仿函数 都叫谓词。
有一个参数 叫 一元谓词。
有二个参数 叫 二元谓词。
示例
1,
使用一元谓词查找大于
30
的数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class GreaterThan30
{
public:
bool operator()(int val)
{
return val>30;
}
};
bool greaterThan30(int val)
{
return val>30;
}
void test02()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//寻找大于30的数
vector<int>::iterator ret;
//仿函数提供策略 需要函数对象
//ret = find_if(v1.begin(), v1.end(), GreaterThan30() );
//普通函数提供策略
ret = find_if(v1.begin(), v1.end(), greaterThan30 );
if(ret != v1.end())
{
cout<<"找到的值:"<<*ret<<endl;
}
}
2.使用二元谓词实现排序
void myPrintInt(int val)
{
cout<<val<<" ";
}
class MyGreater
{
public:
bool operator()(int v1, int v2)
{
return v1>v2;
}
};
bool myGreater(int v1, int v2)
{
return v1>v2;
}
void test03()
{
vector<int> v1;
v1.push_back(30);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(50);
for_each(v1.begin(),v1.end(), myPrintInt);
cout<<endl;
//更改排序策略
//sort(v1.begin(), v1.end(), MyGreater());
sort(v1.begin(), v1.end(), myGreater);
for_each(v1.begin(),v1.end(), myPrintInt);
cout<<endl;
}
内建函数对象
概念
STL提供的函数对象
算法类函数对象
语法
template
<
class
T
>
T plus
<
T
>
//
加法仿函数
template
<
class
T
>
T minus
<
T
>
//
减法仿函数
template
<
class
T
>
T multiplies
<
T
>
//
乘法仿函数
template
<
class
T
>
T divides
<
T
>
//
除法仿函数
template
<
class
T
>
T modulus
<
T
>
//
取模
(
取余
)
仿函数
template
<
class
T
>
T negate
<
T
>
//
取反仿函数
注意
:6
个算数类函数对象
,
除了
negate
是一元运算
,
其他都是二元运算
示例
1,
基本使用
#include<algorithm>
void test04()
{
plus<int> pl;
int num01 = pl(10,20);
cout << "num01 = " << num01 << endl;
int num02 = plus<int>()(1,20);
cout << "num02 = " << num02 << endl;
int num03 = minus<int>()(1,20);
cout << "num03 = " << num03 << endl;
int num04 = multiplies<int>()(1,20);
cout << "num04 = " << num04 << endl;
int num05 = divides<int>()(20,5);
cout << "num05 = " << num05 << endl;
int num06 = modulus<int>()(5,3);
cout << "num06 = " << num06 << endl;
int num07 = negate<int>()(10);
cout << "num07 = " << num07 << endl;
}
2,
本月工资所有员工加
2000
奖金
class Staff{
friend void printStaff(Staff &s);
string name;
double money;
public:
Staff(){}
Staff(string name,double money)
{
this->name = name;
this->money = money;
}
};
void printStaff(Staff &s)
{
double money = plus<double>()(s.money,2000);
cout << s.name << "本月工资为:" << money << endl;
}
void test05()
{
vector<Staff> staffs;
staffs.push_back(Staff("张三",3100));
staffs.push_back(Staff("李四",3000));
staffs.push_back(Staff("王五",3200));
for_each(staffs.begin(),staffs.end(),printStaff);
}
关系运算类函数对象
语法
template
<
class
T
>
bool
equal_to
<
T
>
//
等于
template
<
class
T
>
bool
not_equal_to
<
T
>
//
不等于
template
<
class
T
>
bool
greater
<
T
>
//
大于
template
<
class
T
>
bool
greater_equal
<
T
>
//
大于等于
template
<
class
T
>
bool
less
<
T
>
//
小于
template
<
class
T
>
bool
less_equal
<
T
>
//
小于等
注意
:6
个关系运算类函数对象
,
每一种都是二元谓词
示例
1,
基本使用
void
test06
()
{
cout
<<
"10 == 20 ? "
<<
equal_to
<
int
>
()(
10
,
20
)
<<
endl
;
cout
<<
"20 == 20 ? "
<<
equal_to
<
int
>
()(
20
,
20
)
<<
endl
;
cout
<<
"10 != 20 ? "
<<
not_equal_to
<
int
>
()(
10
,
20
)
<<
endl
;
cout
<<
"10 > 20 ? "
<<
greater
<
int
>
()(
10
,
20
)
<<
endl
;
cout
<<
"