欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

c/c++ 標準庫 bind 函數(shù)詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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

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

auto newCallable = bind(callbale, arg_list);

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

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

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

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

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

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

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

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

bind參數(shù)用法:

//g是以個有2個參數(shù)的可調(diào)用對象
auto g = bind(func, a, b, _2, c, _1);//func是有5個參數(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實現(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改變原來函數(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ù),把對象轉(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++ 標準庫 bind 函數(shù)詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

上一篇:C語言中輸入輸出流與緩沖區(qū)的深入講解

欄    目:C語言

下一篇:c++使用正則表達式提取關(guān)鍵字的方法

本文標題:c/c++ 標準庫 bind 函數(shù)詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/676.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有