c/c++ 標准庫 bind 函數 詳解


標准庫 bind 函數 詳解

bind函數:接收一個函數名作為參數,生成一個新的函數。

auto newCallable = bind(callbale, arg_list);

arg_list中的參數可能包含入_1, _2等,這些是新函數newCallable的參數。

在這篇博客lambda 表達式 介紹 中,討論了find_if的第三個參數的問題,當時是用lambda表達式解決的,有了bind函數后,也可以用bind函數解決。

解決辦法:bind(check_size, _1, sz)

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

其實,newCall= bind(check_size, _1, sz)返回了一個新的函數newCall ,這個newCall 只接受一個參數,正好滿足find_if的要求。

  • 從find_if的角度來看,啊,newCall是含有一個參數的函數,OK,沒問題。
  • 從程序猿的角度看,check_size是含有2個參數的函數,只是提前把sz(6)綁定到了newCall上了,
  • 當調用newCall(s),實際是調用了check_size(s, 6),相當於newCall也有2個參數,只是第二個參數有個默認值為6。newCall(const string &s, size_t sz = 6); ,所以調用newCall時,傳遞一個參數就夠了。

注意:_1,_2等,是放在了命名空間placeholder中,所以要使用:

//_1,_n定在std::placeholders里面
using namespace std::placeholders;

bind參數用法:

//g是以個有2個參數的可調用對象
auto g = bind(func, a, b, _2, c, _1);//func是有5個參數的函數
調用g(X, Y), 等於 func(a, b, Y, c, X)

例子:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;
//_1,_n定在std::placeholders里面                                                
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(){

  /*                                                                            
  //用bind實現了和lambda一樣的功能                                              
  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;                                                         
  */

  /*                                                                            
  //用bind改變原來函數的參數的位置                                              
  //升序                                                                        
  vector<string> svec{"aab","d","aa","bb","e","bbb"};                           
  sort(svec.begin(), svec.end(), shorter);                                      
  for(auto const &s : svec){                                                    
    cout << s << " ";                                                           
  }                                                                             
  cout << endl;                                                                 
  //由於調換了shorter參數的位置,所以變成了降序                                 
  sort(svec.begin(), svec.end(),bind(shorter, _2, _1));                         
  for(auto const &s : svec){                                                    
    cout << s << " ";                                                           
  }                                                                             
  cout << endl;                                                                 
  */

  //bind引用,必須使用ref或者cref函數,把對象轉化成引用,不能用&                 
  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;

}

c/c++ 學習互助QQ群:877684253

本人微信:xiaoshitou5854


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM