当前位置:网站首页>STL -- binder
STL -- binder
2022-06-28 08:42:00 【Jinky strange 18】
What is a binder ? Why is it proposed ? See the following code to understand the source and function of the binder .
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int arr[] = {12,34,42,52,64,62,90,102,46,256,78};
int len = sizeof(arr) / sizeof(arr[0]);
vector<int> vec(arr,arr+len);
// The first query is greater than 100 The elements of Greater than :greater Less than :less
//vector<int>::iterator it = find_if(vec.begin(),vec.end(),(greater<int>(),100));
//greater<int> great;
//great(10,20); The essence of an object is to call member methods ==> great.operator(10,20)
// Pass in two parameters ( This is the binary function object ), Which is bigger
// but find The function requires the first two arguments to be passed into the iterator interval , The third is the unary function object
//greater<int> You need to pass in two function objects How to solve ?
// Rewrite -->no Use binders ( Convert a binary function object to a unary function object )
vector<int>::iterator it = find_if(vec.begin(),vec.end(),bind2nd(greater<int>(),100));
if(it != vec.end())
{
cout << " The first one is greater than 100 The elements of :" << *it << endl;
}
return 0;
}1. Binder Of Realization principle :
Function object It is divided into Unary function object operator(lhs) and Binary function objects operator()(lhs,rhs)
Binder bind2nd(greater<int>(),100) Quite so 100 Bound to the second parameter rhs, namely operator()(lhs,100), The second parameter is given , It is equivalent to directly calling a unary function object , Just change the binary function object into the unary function object
Binary function objects The parameters of are Left operand and Right operands
bind1st( Bind left operand ): Compare the size of all elements with the left operand
bind2nd( Bind right operand ): Compare the size of all elements with the right operand The above example is to combine all elements with 100 Compare , Find the first one 100 Big
2. Binder The concrete realization of :
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
template<typename Comp>// By looking at bind2nd The definition of , Find just one Comp Function object is OK
class MyBinder
{
public:
MyBinder(Comp& f,const typename Comp::second_argument_type& val)
{
fn = f;
value = val;
}
bool operator()(const typename Comp::second_argument_type& first)// Pass in the first argument of the binary function object
{
return fn(first,value);
}
private:
Comp fn;// Comp Class generation fn object , The second member variable needs to be bound with a parameter
typename Comp::second_argument_type value;// The second parameter in the binary function object
};
template<typename Comp,typename T>// Function object and value type are required
MyBinder<Comp> MyBinderSecond(Comp &fn,const T& value)// Templates :MyBinder Class name :MyBinder<Comp> Add parameters to the template to form the class name
{
return MyBinder<Comp>(fn,value);
}
int main()
{
// The first query is greater than 100 The elements of (greater) Query the first less than 40 The elements of (less)
int arr[] = {12,34,42,52,64,62,90,102,46,256,78};
int len = sizeof(arr) / sizeof(arr[0]);
vector<int> vec(arr,arr+len);
// Use your own binder
vector<int>::iterator it1 = find_if(vec.begin(),vec.end(),MyBinderSecond(greater<int>(),100));
vector<int>::iterator it2 = find_if(vec.begin(),vec.end(),MyBinderSecond(less<int>(),40));
if(it1 != vec.end())
{
cout << " The first one is greater than 100 The elements of :" << *it1 << endl;
}
if(it2 != vec.end())
{
cout << " The first one is less than 40 The elements of :" << *it2 << endl;
}
return 0;
}As shown in the figure below , The calling relationship of the binder
(1) Calling point :MyBinderSecond(greater<int>(),100) . take Show temporary objects greater<int>() and value 100 Pass in ,greater The generated object is transferred to the binder written by itself ,greater Pass to fn, value 100 Pass to value
greater Prototypes of function objects : No member variables , There is only one member method
template<typename T>
class greater
{
public:
bool operator()(first,second)
{
return first > second;
}
};(2) Take the two arguments passed externally to build a new function object
Initialize the member variables in the new function object through the binary function object fn , Data binding val Put it in the member variable value in
(3) Now generate a MyBinder The object of , It is equivalent to encapsulating binary function objects , Calling a member method , It can be used by passing only one parameter , Inside fn(first,value) also Call a binary function object , It is equivalent to encapsulating the binary function object

3. Binder The role of : Put the binary function object convert to Unary function object
边栏推荐
- Ffmpeg streaming fails to update header with correct duration
- 个人究竟如何开户炒股?在线开户安全么?
- Almost union find (weighted union search)
- FFMpeg (一) av_register_all()
- Force buckle 1884 Egg drop - two eggs
- break database---mysql
- Superimposed ladder diagram and line diagram and merged line diagram and needle diagram
- RAC enable archive log
- Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
- WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
猜你喜欢

Kubernetes notes and the latest k3s installation introduction

Why MySQL cannot insert Chinese data in CMD

Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.

【无标题】

High rise building fire prevention

Privacy computing fat----- offline prediction

What is the bandwidth of the Tiktok server that can be used by hundreds of millions of people at the same time?

Build an integrated kubernetes in Fedora

如何抑制SiC MOSFET Crosstalk(串扰)?

PMP从报考到拿证基本操作,了解PMP必看篇
随机推荐
Super Jumping! Jumping! Jumping!
Infinite penetration test
Dell r730 server startup error: [xxx] USB 1-1-port4: disabled by hub (EMI?), re-enabling...
Power data
Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
Selenium reptile
WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
抖音服務器帶寬有多大,才能供上億人同時刷?
[introduction to SQL for 10 days] day4 Combined Query & specified selection
centos mysql5.5配置文件在哪
[go ~ 0 to 1] on the first day, June 24, variables, conditional judgment cycle statement
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
电子元器件销售ERP管理系统哪个比较好?
It only takes two steps to find the right PMP organization, one check and two questions
如何抑制SiC MOSFET Crosstalk(串擾)?
Build an integrated kubernetes in Fedora
MySQL8.0 忘记 root 密码
High rise building fire prevention
Why MySQL cannot insert Chinese data in CMD
Kali installation configuration