重载函数调用操作的类,其对象常称之为函数对象;
函数对象使用重载()时,其行为类似函数调用,也叫仿函数;
函数对象(仿函数)本质是一个类,不是一个函数。
函数对象在使用时可以有形参、有返回值。
函数对象可以有自己的状态值。
函数对象可以作为函数形参。
#include
using namespace std;
class myfunc
{
public:
myfunc()
{
count = 0;
}
//求和示例,重载()
int operator()(int a, int b)
{
return a + b;
}
//输出示例,count记录函数调用次数
void operator()(string str)
{
count++;
cout << str << endl;
}
int count;
};
void print(myfunc& p, string test)
{
p(test);
}
void test()
{
//创建一个函数对象
myfunc p1;
cout << "\t函数对象形参返回使用示例:" << endl;
int ret=p1(10, 20);
cout << "ret=" << ret << endl;
cout << "\t仿函数重载示例:" << endl;
p1("C++学习--仿函数使用示例!");
p1("C++学习--仿函数使用示例!");
p1("C++学习--仿函数使用示例!");
cout << "函数调用次数:" << p1.count << endl;
cout << "\t仿函数作为函数形参:" << endl;
print(p1, "hello,欢迎学习c++课程");
}
int main()
{
test();
system("pause");
}
函数对象返回值为bool类型,则称之为谓词;
仿函数的形参只有一个;
仿函数的形参有两个参数;
#include
#include
#include
using namespace std;
class Check
{
public:
bool operator()(int val)
{
return val > 5;
}
bool operator()(int a1,int a2)
{
return a1 > a2;
}
};
void test()
{
vector<int>vtr;
/*插入数据*/
for (int i = 0; i < 10; i++)
{
vtr.push_back(i);
}
cout << "一元谓词示例:查找vector容器中>5的值" << endl;
/*查找vector容器中>5的值*/
vector<int>::iterator ret=find_if(vtr.begin(), vtr.end(), Check());//Check() ---匿名函数对象
if (ret ==vtr.end())
{
cout << "未查到到>5的值!" << endl;
}
else
{
cout << "查找成功,大于5的值为:" <<*ret<<endl;
}
cout << "二元谓词示例:对vector容器按从大到小顺序排序" << endl;
sort(vtr.begin(), vtr.end(), Check());
for (vector<int>::iterator ptr = vtr.begin(); ptr != vtr.end(); ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
int main()
{
test();
system("pause");
}
STL中提供了一些内建函数对象:算术仿函数、关系仿函数、逻辑仿函数 --头文件;
注意:其中negate是一元运算(只有一个参数),其余均为二元运算。
#include
using namespace std;
#include
void test()
{
//negate使用示例:
negate<int> n;
cout << "negate取反示例:" << n(188) << endl;
plus<int> p;
cout << "plus加法:" << p(10, 20) << endl;
minus<float>m;
cout << "minus减法取绝对值:" << n(m(10, 20)) << endl;
multiplies<float>mt;
cout << "multiplies乘法:" << mt(5, 3.15) << endl;
divides<float>d;
cout << "divides除法:" << d(10, 3) << endl;
modulus<int>md;
cout << "modulus取模:" << md(10, 3) << endl;
}
int main()
{
test();
system("pause");
}
内建仿函数:关系运算符
大于: templatebool greater
大于等于:templatebool greater_equal
小于: templatebool less
小于等于:templatebool less_equal
等于: templatebool equal_to
不等于: templatebool not_equal_to
#include
using namespace std;
#include
#include
#include
void print(int val)
{
cout << val << " ";
}
int main()
{
vector<int> vtr;
vtr.push_back(10);
vtr.push_back(40);
vtr.push_back(30);
vtr.push_back(60);
vtr.push_back(6);
/*sort排序,默认是从小到大,其默认的仿函数即less*/
sort(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), print);
cout << endl;
/*
要实现从大小,可以自行实现一个仿函数
class mycompare
{
public:
bool operator()(int a1,int a2)
{
return a1>a2;
}
}
也可以直接使用STL内建仿函数:greater()
*/
sort(vtr.begin(), vtr.end(), greater<int>());
for_each(vtr.begin(), vtr.end(), print);
cout << endl;
system("pause");
}
内建仿函数–逻辑运算符
逻辑与:templatebool logical_and
逻辑或: templatebool logical_or
逻辑非: templatebool logical_not
#include
using namespace std;
#include
#include
#include
void print(bool val)
{
cout << val << " ";
}
void test()
{
vector<bool> vtr;
vtr.push_back(true);
vtr.push_back(true);
vtr.push_back(false);
vtr.push_back(false);
vector<bool>vtr2;
vtr2.resize(vtr.size());//设置vtr2的容器大小
//将vtr容器内容取非放到vtr2中
transform(vtr.begin(), vtr.end(), vtr2.begin(), logical_not<bool>());
for_each(vtr.begin(), vtr.end(), print);
cout << endl;
for_each(vtr2.begin(), vtr2.end(), print);
cout << endl;
}
int main()
{
test();
system("pause");
}