当前位置:网站首页>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
边栏推荐
- Power data
- JS rounding tips
- Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
- Set the encoding of CMD to UTF-8
- Maintenance and protection of common faults of asynchronous motor
- 【转载】STM32 GPIO类型
- Zhejiang energy online monitoring and management system
- Idea related issues
- Redis deployment under Linux & redis startup
- 【云原生 | Kubernetes篇】深入了解Pod(六)
猜你喜欢

找合适的PMP机构只需2步搞定,一查二问

与普通探头相比,差分探头有哪些优点

Kubernetes notes and the latest k3s installation introduction

Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?

电子元器件销售ERP管理系统哪个比较好?

Mysql8.0 forgot the root password

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

Construire le premier réseau neuronal avec pytorch et optimiser

Almost Union-Find(带权并查集)

隐私计算FATE-----离线预测
随机推荐
Loss loss function
状态机程序框架
Why are function templates not partial specialization?
Build an integrated kubernetes in Fedora
887. egg drop
隐私计算FATE-----离线预测
nuxt3入门
抖音服務器帶寬有多大,才能供上億人同時刷?
Set<String>
Super Jumping! Jumping! Jumping!
Redis deployment under Linux & redis startup
【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
Guangzhou: new financial activities and new opportunities for enterprises
40多岁的人如何配置年金险?哪款产品比较合适?
Trailing Zeroes (II)
Hidden scroll bar on PC
[introduction to SQL for 10 days] day4 Combined Query & specified selection
What is the bandwidth of the Tiktok server that can be used by hundreds of millions of people at the same time?
Build the first neural network with pytoch and optimize it
[learning notes] simulation