定义f函数有五个int参数,并使用bind函数绑定f函数。 代码如下。
# include iostream # include functional//bind函数functional头文件中的# includealgorithmusingnamespacestd; intf(inta、int b、int x、int c、int y ); //函数boolisshorter(charS1,char s2 ) { return s1 s2; }int main () { string str='Hello World '; STD :3360 coutstrstd :3360 endl; //控制台上安装12345autog=STD:bind(f,1,2,STD 33603360 placeholder s 33603360 _ 2,4,STD3360:placeholders3360 //g (3,5 )的第一个实参加入占位符_1绑定,第二个实参5加入占位符_2绑定stringwords('3254617 ' ); sort(Words.Begin )、words.end )、isShorter ); 计数words endl; 字符串words2(3254617 ); sort(words2.Begin ),words2.end ),STD:3360bind(isshorter,STD 33603360 placeholder s 33603360 (2,STD 3360: cold (intf ) inta、int b、int x、int c、int y ) ) { cout a b x c y endl; 返回0; }执行g (3,5 ),发现有以下输出。
得出结论:传递给g的参数按位置绑定到占位符,即,第一个参数绑定到_1,第二个参数绑定到_2。