弄月acome分节阅读,sqte-021,好心情随想日记
auto newcallable = bind(callbale, arg_list);
auto idx = find_if(svec.begin(),svec.end(),bind(check_size, _1, 6));
其实,newcall= bind(check_size, _1, sz)返回了一个新的函数newcall ,这个newcall 只接受一个参数,正好满足find_if的要求。
//_1,_n定在std::placeholders里面 using namespace std::placeholders;
//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; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论