当前位置:网站首页>Function object (functor)
Function object (functor)
2022-06-09 22:02:00 【Gy648】
Function object
Function object ( functor )
Function objects can be called like ordinary functions when used , There can be parameters , You can have a return value
Function objects can go beyond the concept of ordinary functions , Function objects can have their own states
Function objects can be passed as arguments
class myadd
{
public:
int operator()(int v1,int v2) // Overloading function callers
{
return v1+v2;
}
};
- Function objects are used , You can call... Like a normal function
void test01()
{
myadd myadd;
cout<<myadd(10,10)<<endl;
}
- Function objects can have their own state
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;
}
Different from ordinary functions , A mock function is not called like an ordinary function , Destroyed as the call ends , You can have your own internal records
- Function objects can be passed as arguments
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");
}
The predicate
return bool A functor of type is called a predicate
If opertor() Take a parameter , Is a unary predicate
Two parameters , Is a binary predicate
Unary predicate
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()); // Pass in an anonymous function object
// The return value is the iterator position , Find one greater than 5 The number of
}
two-place predicate
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());
}
边栏推荐
- How to implement a custom rich text editor label
- spider pi 智能视觉六足机器人自动避障 0604
- Basic use of DataGridView 0526
- Day5-t2029 & T39 -2022-01-20-not answer by yourself
- 202206007 模拟赛 总结
- leetcode:547、朋友圈
- Oracle paging
- mysql(mariadb)无法打开,报错:找不到mysqld.sock且Can‘t connect to MySQL server on 127.0.0.1(111)
- 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)
- Application of queue in process pool
猜你喜欢
Database SQL and Gorm practice of design pattern | notes on youth training camp

Spider PI intelligent vision hexapod robot in direct connection mode 0603

spider pi 智能视觉六足机器人自动避障 0604

winform编程 控件TreeView树视图的基本使用 20220527

Mqtt graphical client-mqttx installation and use tutorial

PaddleNLP通用信息抽取技术UIE【一】产业应用实例:信息抽取{实体关系抽取、中文分词、精准实体标。情感分析等}、文本纠错、问答系统、闲聊机器人、定制训练

Unity get the content information of XML file

The 14th Sudoku - true standard Sudoku -day 6-20220121 (supplementary)

The 14th Sudoku - true Sudoku - day 5-20220120

MATLAB实现Pettitt突变检验
随机推荐
如何定比特到服務器CPU飆高的原因
Highly recommended: data annotation platform doccano - introduction, installation, use and pit stepping records
深入理解 Go Modules 的 go.mod 與 go.sum
Load balancing configuration of EIGRP for daily technology sharing
js 自增和自减(一元运算符)
scratch编程 飞翔的小鸟 开发笔记 0604
mysql(mariadb)无法打开,报错:找不到mysqld.sock且Can‘t connect to MySQL server on 127.0.0.1(111)
Mqtt graphical client-mqttx installation and use tutorial
A thorough understanding of the very important ticket lock in concurrent programming -- stampedlock
MFC connection database shows no data source name found and no default driver specified
Basic use of DataGridView 0526
The application of prototype object strengthens the function of array object 0526
[code audit] Buu_ [GWCTF 2019]mypassword
Daily 2 Trivia^_^
YOLO系列目标检测后处理-非极大值抑制
Day5-t2029 & T39 -2022-01-20-not answer by yourself
How image search works in Dropbox
What is the difference between a WiFi 6 router and a WiFi 5 router
Embedded software design (interim summary)
邦纳雷达传感器Q120RAQ-CN-AF19719