函数对象
#include<iostream>
using namespace std;
//函数对象―(仿函数)
/*
-1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
-2.函数对象超出普通函数的概念,函数对象可以有自己的状态
-3.函数对象可以作为参数传递
*/
//-1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd {
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test01()
{
MyAdd add;
cout << "两数相加后结果:" <<add(34, 78) << endl;
}
//-2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class Print
{
public:
Print()
{
this->count = 0;
}
void operator()(string text)
{
cout << text << endl;
this->count++;
}
int count;
};
void test02()
{
Print p;
p("hello");
p("hello");
p("hello");
p("hello");
cout <<"Print 调用次数"<<p.count << endl;
}
//-3.函数对象可以作为参数传递
void DoPrint(Print p, string text) {
p(text);
};
void test03()
{
Print p;
DoPrint(p, "你好, yys");
}
int main()
{
//test01();
test02();
test03();
system("pause");
return 0;
}
//总结,仿函数写法非常灵活,可以作为参数进行传递
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class GreatFive {
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector <int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中 有没有大于5个数字
//GreatFive()匿名函数对象
vector <int>::iterator it = find_if(v.begin(), v.end(), GreatFive());
if (it != v.end())
{
cout << "找到满足大于5的数:" <<*it<< endl;
}
else
{
cout<<"没找到"<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:参数只有一个谓词,称为一元谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class Mysort {
public:
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
void test01()
{
vector<int> v;
v.push_back(33);
v.push_back(23);
v.push_back(47);
v.push_back(17);
v.push_back(90);
sort(v.begin(), v.end());
cout << "从小到大排序" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "从大到小排序" << endl;
//使用函数对象,改变算法策略,变为排序规则从大到小
sort(v.begin(), v.end(), Mysort());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:参数只有两个的谓词,称为二元谓词
#include<iostream>
using namespace std;
#include<functional>
/*
template<class T> T plus<T>//加法仿函数
template<class T> T minus<T>//减法仿函数
template<class T> T multipies<T>//乘法仿函数
template<class T> T divides<T>//除法仿函数
template<class T> T modulus<T>//取模仿函数
template<class T> T negate<T>//取反仿函数
*/
//一元仿函数 取反
void test01()
{
negate<int> n;
cout << n(23) << endl;
}
//二元仿函数 加减乘除
void test02()
{
plus<int> p;
cout << p(23, 32) << endl;
//减乘除类似
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
//总结:使用内建函数时,需要加入头文件#include<functional>
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void test01()
{
vector<int> v;
v.push_back(14);
v.push_back(45);
v.push_back(23);
v.push_back(65);
v.push_back(34);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//greater<int>()内建函数对象
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:关系仿函数中最常用的就是greater<>大于
评论列表 (0 条评论)