当前位置:网站首页>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
边栏推荐
- Goldbach`s Conjecture
- DB
- Tree
- Selenium reptile
- Basic twelve style classes for duilib
- Large current and frequency range that can be measured by Rogowski coil
- Super Jumping! Jumping! Jumping!
- Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
- 用Pytorch搭建第一個神經網絡且進行優化
- Guangzhou: new financial activities and new opportunities for enterprises
猜你喜欢

【大案例】学成在线网站

Construire le premier réseau neuronal avec pytorch et optimiser

用Pytorch搭建第一个神经网络且进行优化

The 6th smart home Asia 2022 will be held in Shanghai in October

MySQL8.0 忘记 root 密码

What are the advantages of a differential probe over a conventional probe

Discussion on the improvement and application of the prepayment system in the management of electricity charge and price

Set the icon for the title section of the page

Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)

TCP那点事
随机推荐
Loss损失函数
Tree
隐私计算FATE-----离线预测
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
A - Bi-shoe and Phi-shoe
Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
Idea related issues
Construire le premier réseau neuronal avec pytorch et optimiser
How do individuals open accounts to speculate in stocks? Is online account opening safe?
【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
High rise building fire prevention
Cloudcompare & PCL point cloud SVD decomposition
Tree
Understanding of CUDA, cudnn and tensorrt
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
Infinite penetration test
Set the encoding of CMD to UTF-8
[reprint] STM32 GPIO type
AWS saves data on the cloud (3)