当前位置:网站首页>函数对象(仿函数)
函数对象(仿函数)
2022-06-09 21:18:00 【Gy648】
函数对象(仿函数)
函数对象在使用时可以向普通函数那样调用,可以有参数,可以有返回值
函数对象可以超出普通函数的概念,函数对象可以有自己的状态
函数对象可以作为参数传递
class myadd
{
public:
int operator()(int v1,int v2) //对函数调用符重载
{
return v1+v2;
}
};
- 函数对象在使用时,可以向普通函数一样调用
void test01()
{
myadd myadd;
cout<<myadd(10,10)<<endl;
}
- 函数对象可以拥有自己的状态
class myprint
{
public:
myprint()
{
this->count=0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count = 0;
};
void test01()
{
myprint myprint;
myprint("hello");
myprint("hello");
myprint("hello");
myprint("hello");
myprint("hello");
cout<<" count = "<<myprint.count<<endl;
}
与普通函数不同,仿函数在调用时不会像普通函数一样,随着调用的结束被销毁,可以有自己内部的记录
- 函数对象可以作为参数传递
class myprint
{
public:
myprint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count = 0;
};
void doprint(myprint &mp, string test)
{
mp(test);
}
void test01()
{
myprint myp;
doprint(myp, "aaaaaa");
}
谓词
返回bool类型的仿函数称为谓词
如果opertor()接受一个参数,则为一元谓词
两个参数,则为二元谓词
一元谓词
class overfive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), overfive());
find_if(v.begin(), v.end(), overfive()); //传入匿名的函数对象
//返回值为迭代器位置,找到一个大于5的数就终止
}
二元谓词
class mycomper
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(45);
v.push_back(21);
v.push_back(35);
v.push_back(12);
sort(v.begin(), v.end(),mycomper());
}
边栏推荐
- Yolo series target detection post-processing - non maximum suppression
- js基本数据类型和引用数据类型
- Deploy MySQL based on statefulset in kubernetes (Part 2)
- Implementation principle and best practice of big data ecological security framework (Part I)
- TL, how do you manage project risks?
- Laravel8 use when search
- 10 minute quick start RDS [Huawei cloud to jianzhiyuan]
- Database daily question - day 7: customers with the most orders
- Spider PI intelligent vision hexapod robot unpacking 0602
- spider pi 智能视觉六足机器人 开箱介绍 0602
猜你喜欢

List of database import and database structure for the development of yimai.com 0605

Spider PI intelligent vision hexapod robot patrol function 0603

STM32 memory knowledge

Second cloud cloud's original fully compatible solution for information innovation promotes the acceleration of the implementation of information innovation industry

swagger上的model和返回的数据字段不一致的问题

Basic use of DataGridView 0526

How to implement a custom rich text editor label

能源设备智能化系统平台建设 红外抄表项目案例

兰吉尔FFC3 电表电能量采集终端替代方案(DLMS红外光电采集器)

发电厂企业的关口表参数里的组合无功1和组合无功2的含义--抄表数采问题
随机推荐
PaddleNLP--UIE(二)--小样本快速提升性能(含doccona标注)
Paddlenlp--uie (II) -- fast performance improvement with small samples (including doccona label)
es5中构造函数的属性继承 借用父构造函数 方法继承 原型对象
蓝牙协议分析(7)_BLE连接有关的技术分析
Spider PI intelligent vision hexapod robot tag recognition apirltag tag 0604
Spider PI intelligent vision hexapod robot unpacking 0602
PHP uses unlink to delete the file prompt without permission
JS cast and implicit type conversion and Unicode encoding
How to make our self built site have a custom domain name (1)
BLE链路层空中包格式
What are the advantages of realizing enterprise digital transformation with odoo?
Thinking of finding the k-th largest element in O (n) by fast sorting
Sqlserver2012 does not allow to modify the table structure 0607
浅谈倍增法求解LCA
spider pi 智能视觉六足机器人 vnc连接机器人 0603
winform编程 控件TreeView树视图的基本使用 20220527
强烈推荐:数据标注平台doccano----简介、安装、使用、踩坑记录
spider pi 智能视觉六足机器人 直连模式下进行试玩 0603
MySQL (MariaDB) cannot be opened. An error is reported: mysqld cannot be found Sock and can't connect to MySQL server on 127.0.0.1 (111)
js 强制类型转换 和 隐式类型转换 和 Unicode编码