【C++】bind参数绑定 P354(通用的函数适配器)
生活随笔
收集整理的這篇文章主要介紹了
【C++】bind参数绑定 P354(通用的函数适配器)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 什么時候該使用bing ?什么時候該使用lambda?
-
當只有少數地方調用時候使用lambda,當需要多次調用lambda時,需要定義一個函數,而不是多次編譯相同的lambda表達式。
調用bind的一般形式為: auto newCallable = bind(callable,arg_list) callable本身是一個可調用的對象,arg_list是一個逗號分隔的參數列表,對應給定的callable的參數。即當調用newCallable時,newCallable會調用callable,并傳遞給arg_list中的參數。arg_list中的參數可能包含形如 _n 的名字,其中 n是一個整數。這些參數是“占位符”,表示newCallable的參數,它們占據了傳遞給newCallable的參數的“位置”。 數值 n 表示生成的可調用對象中參數的位置: _1 為newCallable的第一個參數,_2為newCallable的第二個參數,依次類推。2.實例
demo1: bind的使用實例
bool check_size(const string& s, string::size_type sz) {return s.size() >= sz;}string s = "hello";auto check6 = bind(check_size, std::placeholders::_1, 6);bool b1 = check6(s); std::cout << b1 << std::endl;demo2: for_each find_if stable_sort使用實例
vector<string> wordVec = { "the","quick","red","fox","jnmp","slow","over","the","red" };//the red fox the red jnmp slow over quickstable_sort(wordVec.begin(), wordVec.end(),[](const string& a, const string& b){return a.size() < b.size(); });vector<string>::size_type sz = 4;auto wc = find_if(wordVec.begin(), wordVec.end(),[sz](const string& a){return a.size() >= sz; });// jump std::cout << *wc << std::endl;// jnmp slow over quickauto count = wordVec.end() - wc;for_each(wc, wordVec.end(), [](const string& s) { cout << s << " "; });demo 3:通過bind 實現find_if調用check_size適配實現
auto wc = find_if(wordVec.begin(), wordVec.end(),bind(check_size,std::placeholders::_1,sz));3. bind 參數:bind綁定可調用對象中參數或將其重新排序
// g 是一個有2個參數的可調用對象,f是一個可調用對象,他有5個參數 auto g = bind(f,a,b,_2,c,_1); 如上,生成一個新的可調用對象,它有2個參數,對別使用占位符 _2 和 _1表示,這個新的可調用對象將自己的參數作為第三個和第五個傳遞給f。f的第一個、第二個和第四個數參數分別綁定到給定的值 a,b,c上傳遞給g的參數按位置綁定到占位符。即第一個參數綁定到_1,第二個參數綁定到_2。當調用g時,其第一個參數將被傳遞給f作為最后一個參數,第二個參數將被傳遞給f作為第三個參數實際上,這個bind調用會將 g(_1,_2) 映射為 f(a,b,_2,c,_1) 即對g的調用會調用f,用g的參數代替占位符,再加上綁定的參數a,b,c。例如: 調用g(X,Y) 映射為 f(a,b,Y,c,X)4. 用bind重排參數順序
-
通用寫法
bool isShorter(const string& s1, const string& s2) {return s1.size() < s2.size(); } -
lambda寫法
stable_sort(wordVec.begin(), wordVec.end(),[](const string& a, const string& b){return a.size() < b.size(); }); -
bind 寫法,由短至長排序
sort(wordVec.begin(), wordVec.end(), isShorter); -
bind寫法,由長至短排序
std::stable_sort(wordVec.begin(), wordVec.end(), std::bind(isShorter,std::placeholders::_2, std::placeholders::_1));
5. 綁定引用參數
-
5.1 默認情況下,bind那些不是占位符的參數,被拷貝到bind返回的可調用對象當中。,有時對有些綁定的參數可以采用引用的方式,或者要綁定參數的類型無法拷貝。
錯誤實例:os不能拷貝,原因在于bind拷貝其參數,bind不能直接拷貝ostream對象
for_each(a.begin(),a.end(),bind(print,os,_1,' '));標準庫ref函數,可以實現bind傳遞一個對象而不拷貝該對象
for_each(a.begin(),a.end(),bind(print,ref(os),_1,' '));函數ref返回一個對象,包含給定的引用,此對象是可以拷貝的。
-
5.2 demo2 RemapPointsInternal計算實例
if (configuration_->threads() > 1) {tbb::parallel_for((size_t) 0,(size_t) point_centers_remapped.size(),(size_t) 1,std::bind(&Remapper::RemapPointsInternal,this,std::ref(point_centers_remapped),std::ref(point_centers),std::ref(in_out_img_extended_flag),std::ref(in_out_img_origin_flag),std::cref(forword_map_x),std::cref(forword_map_y),x_offset,y_offset,temperature_ntc,std::cref(calib_params),std::placeholders::_1));} else {for (size_t i = 0; i < point_centers_remapped.size(); ++i) {RemapPointsInternal(point_centers_remapped,point_centers,in_out_img_extended_flag,in_out_img_origin_flag,forword_map_x,forword_map_y,x_offset,y_offset,temperature_ntc,calib_params,i);}} -
5.3 demo3 描述子計算實例ComputeDescriptorsInternal
if (configuration_->threads() > 1) {tbb::parallel_for((size_t) 0,(size_t) points.size(),(size_t) 1,std::bind(&DescriptorGenerator::ComputeDescriptorsInternal<PointType>,this,std::ref(points),std::ref(descriptors),std::ref(point_centers_remap),std::ref(bit_descriptors),std::ref(point_center_image),search_offsets_fxp,search_angle,coarse_quadrant,std::placeholders::_1));} else {for (size_t i = 0; i < points.size(); ++i)ComputeDescriptorsInternal(points,descriptors,point_centers_remap,bit_descriptors,point_center_image,search_offsets_fxp,search_angle,coarse_quadrant,i);}
使用placeholders名字
-
-
1.通過 using 語句,而不是分別聲明每個占位符 格式如下
using namespace namespace_name實例:
using namespace std::placeholders;/ PLACEHOLDER ARGUMENTS namespace placeholders {_INLINE_VAR constexpr _Ph<1> _1{};_INLINE_VAR constexpr _Ph<2> _2{};_INLINE_VAR constexpr _Ph<3> _3{};_INLINE_VAR constexpr _Ph<4> _4{};_INLINE_VAR constexpr _Ph<5> _5{};........._INLINE_VAR constexpr _Ph<17> _17{};_INLINE_VAR constexpr _Ph<18> _18{};_INLINE_VAR constexpr _Ph<19> _19{};_INLINE_VAR constexpr _Ph<20> _20{}; } // namespace placeholders
總結
以上是生活随笔為你收集整理的【C++】bind参数绑定 P354(通用的函数适配器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C++】lambda 表达式
- 下一篇: 关于电视剧。