c and c++ Standard library bind function details

  • 2020-06-07 04:59:46
  • OfStack

bind functions are defined in the header file functional. Think of the bind function as a general-purpose function adapter that accepts a callable object and generates a new callable object to "fit" the original object's argument list.

bind function: Takes a function name as an argument and generates a new function.


auto newCallable = bind(callbale, arg_list);

The arguments in arg_list may include _1, _2, and so on, which are arguments to the new function newCallable.

In this blog introduction to lambda expressions, we discuss the third parameter of find_if, which was solved by the lambda expression, or bind with the bind function.

Solutions: bind(check_size, _1, sz)


auto idx = find_if(svec.begin(),svec.end(),bind(check_size, _1, 6));

In fact, newCall= bind(check_size, _1, sz) A new function, newCall, is returned. This newCall takes only one argument, which is exactly what find_if requires.

The & # 8226; From the point of view of find_if, ah, newCall is a function with one argument, OK, no problem.
The & # 8226; From the programmer's point of view, check_size is a function with two parameters, but it binds sz(6) to newCall in advance.
The & # 8226; When calling newCall(s), it actually calls check_size(s, 6), which is equivalent to newCall also has two parameters, but the second parameter has a default value of 6. newCall (const string & s, size_t sz = 6); , so when newCall is called, passing one argument is enough.

Note: _1,_2, and so on are placed in the namespace placeholder, so use:


//_1,_n Set in the std::placeholders inside 
using namespace std::placeholders;

bind parameter usage:


//g Based on a 2 A callable object with three parameters 
auto g = bind(func, a, b, _2, c, _1);//func There is a 5 A function of three parameters 

Call g(X, Y) to func(a, b, Y, c, X)

Example:


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
//_1,_n Set in the std::placeholders inside                         
using namespace std::placeholders;
bool check_size(const string &s, string::size_type sz){
 return s.size() >= sz;
}
bool shorter(const string &a, const string &b){
 return a.size() < b.size();
}
ostream& print(ostream& os, const string &s, const char &c){
 //c = ',';                                  
 return os << s << c;
}
int main(){
 /*                                      
 // with bind Implements and lambda1 The function of the sample                        
 vector<string> svec{"aab","d","aa","bb","e","bbb"};              
 stable_sort(svec.begin(),svec.end(),[](const string &a, const string &b){   
   return a.size() < b.size();                        
  });                                     
 string::size_type sz = 3;                           
 auto idx = find_if(svec.begin(),svec.end(),bind(check_size, _1, sz));     
 cout << *idx << endl;                             
 idx = find_if(svec.begin(),svec.end(),[sz](const string &s){         
   return s.size() >= sz;                          
  });                                     
 cout << *idx << endl;                             
 */
 /*                                      
 // with bind Change the position of the parameters of the original function                        
 // ascending                                     
 vector<string> svec{"aab","d","aa","bb","e","bbb"};              
 sort(svec.begin(), svec.end(), shorter);                   
 for(auto const &s : svec){                          
  cout << s << " ";                              
 }                                       
 cout << endl;                                 
 // Because of the switch shorter Parameter, so it's in descending order                  
 sort(svec.begin(), svec.end(),bind(shorter, _2, _1));             
 for(auto const &s : svec){                          
  cout << s << " ";                              
 }                                       
 cout << endl;                                 
 */
 //bind reference , You must use the ref or cref Function that converts an object to a reference &         
 ostream &os = cout;
 const char c = ' ';
 vector<string> svec{"aab","d","aa","bb","e","bbb"};
 for_each(svec.begin(),svec.end(),[&os, c](const string &s){
   os << s << c;
  });
 os << endl;
 for_each(svec.begin(),svec.end(),bind(print, ref(os), _1, cref(c)));
 os << endl;
 cout << c << endl;
}

conclusion


Related articles: