std::bind
std::bind(callable, arg_list)
#include <iostream>
#include <string>
#include <algorithm>
#include <functional> //std::bind(callable, arg_list)
using namespace std;
using namespace std::placeholders;
bool check_size(const string &s, string::size_type sz){
return s.size() >= sz;
}
bool isShorter(char a, char b){
return a < b;
}
ostream &print(ostream &os, char c, char s){
return os << s << c;
}
int main(int argc, char* argv[]){
string s = "hello";
check_size(s, 6);
//参数绑定
auto check = bind(check_size, _1, 6);
check(s);
auto check2 = bind(check_size, _2, _1);
check2(6,s);
//
sort(s.begin(), s.end(), isShorter);
cout << s << endl;
sort(s.begin(), s.end(), bind(isShorter,_2,_1));
cout << s << endl;
ostream &os = cout;
for_each(s.begin(), s.end(),bind(print, ref(os), _1, ' '));//需要使用标准库的ref()进行绑定引用参数
cout << endl;
for_each(s.begin(),s.end(),[&os,s](char c) mutable {s=" "; os << s << c;});
cout << endl;
auto print2 = [&os,s](char c) mutable {s=" "; os << s << c;};
for_each(s.begin(),s.end(),bind(print2 , _1));
return 0;
}