当前位置:网站首页>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
边栏推荐
猜你喜欢

Construire le premier réseau neuronal avec pytorch et optimiser

罗氏线圈可以测量的大电流和频率范围

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

Set the encoding of CMD to UTF-8

TCP

MySQL8.0 忘记 root 密码

Kubernetes notes and the latest k3s installation introduction
![[cloud native | kubernetes] in depth understanding of pod (VI)](/img/ae/f16f5c090251ab603b88ddadff7eb3.png)
[cloud native | kubernetes] in depth understanding of pod (VI)

Idea related issues

如何抑制SiC MOSFET Crosstalk(串擾)?
随机推荐
[introduction to SQL in 10 days] day5+6 consolidated table
A - Bi-shoe and Phi-shoe
Build the first neural network with pytoch and optimize it
[reprint] STM32 GPIO type
【无标题】
三体攻击(三维拆分加二分)
[go ~ 0 to 1] the next day, June 25, switch statement, array declaration and traversal
中金财富开户安全吗?怎么收费?
Almost union find (weighted union search)
Kali Notes(1)
[learning notes] matroid
Loss损失函数
Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
Login common test case
Sword finger offer 03 Duplicate number in array
Super Jumping! Jumping! Jumping!
Avframe Memory Management API
Not so Mobile
Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
网上炒股开户安不安全?