亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解

 更新時(shí)間:2018年09月20日 09:20:36   作者:小石王  
bind是一組用于函數(shù)綁定的模板。在對(duì)某個(gè)函數(shù)進(jìn)行綁定時(shí),可以指定部分參數(shù)或全部參數(shù),也可以不指定任何參數(shù),還可以調(diào)整各個(gè)參數(shù)間的順序。這篇文章主要介紹了c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù) ,需要的朋友可以參考下

bind函數(shù)定義在頭文件 functional 中??梢詫?bind 函數(shù)看作一個(gè)通用的函數(shù)適配器,它接受一個(gè)可調(diào)用對(duì)象,生成一個(gè)新的可調(diào)用對(duì)象來(lái)“適應(yīng)”原對(duì)象的參數(shù)列表。

bind函數(shù):接收一個(gè)函數(shù)名作為參數(shù),生成一個(gè)新的函數(shù)。

auto newCallable = bind(callbale, arg_list);

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

在這篇博客lambda 表達(dá)式 介紹 中,討論了find_if的第三個(gè)參數(shù)的問(wèn)題,當(dāng)時(shí)是用lambda表達(dá)式解決的,有了bind函數(shù)后,也可以用bind函數(shù)解決。

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

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

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

•從find_if的角度來(lái)看,啊,newCall是含有一個(gè)參數(shù)的函數(shù),OK,沒(méi)問(wèn)題。
•從程序猿的角度看,check_size是含有2個(gè)參數(shù)的函數(shù),只是提前把sz(6)綁定到了newCall上了,
•當(dāng)調(diào)用newCall(s),實(shí)際是調(diào)用了check_size(s, 6),相當(dāng)于newCall也有2個(gè)參數(shù),只是第二個(gè)參數(shù)有個(gè)默認(rèn)值為6。newCall(const string &s, size_t sz = 6); ,所以調(diào)用newCall時(shí),傳遞一個(gè)參數(shù)就夠了。

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

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

bind參數(shù)用法:

//g是以個(gè)有2個(gè)參數(shù)的可調(diào)用對(duì)象
auto g = bind(func, a, b, _2, c, _1);//func是有5個(gè)參數(shù)的函數(shù)

調(diào)用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實(shí)現(xiàn)了和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改變?cè)瓉?lái)函數(shù)的參數(shù)的位置                       
 //升序                                    
 vector<string> svec{"aab","d","aa","bb","e","bbb"};              
 sort(svec.begin(), svec.end(), shorter);                   
 for(auto const &s : svec){                          
  cout << s << " ";                              
 }                                       
 cout << endl;                                 
 //由于調(diào)換了shorter參數(shù)的位置,所以變成了降序                 
 sort(svec.begin(), svec.end(),bind(shorter, _2, _1));             
 for(auto const &s : svec){                          
  cout << s << " ";                              
 }                                       
 cout << endl;                                 
 */
 //bind引用,必須使用ref或者cref函數(shù),把對(duì)象轉(zhuǎn)化成引用,不能用&         
 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;
}

總結(jié)

以上所述是小編給大家介紹的c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論