C++ のバインド関数とプレースホルダー
場合によっては、必要に応じて関数の操作を操作する必要があります。つまり、一部の引数をデフォルトに変更するなどです。 デフォルトの引数 関数の汎用性が制限され、デフォルトの引数を使用する必要があり、それも毎回同様の値を使用する必要があります。 C++11 以降では、bind 関数の導入により、このタスクが簡単になりました。
bind() はどのように機能するのでしょうか?
プレースホルダーを利用したバインド関数は、関数で使用される値の位置と数を操作し、目的の出力に応じて関数を変更するのに役立ちます。
プレースホルダーとは何ですか?
プレースホルダーは、関数内の値の位置を指示する名前空間です。それらは次のように表されます。 _1 _2 _3 ...
例:
CPP // C++ code to demonstrate bind() and // placeholders #include #include // for bind() using namespace std ; // for placeholders using namespace std :: placeholders ; // Driver function to demonstrate bind() void func ( int a int b int c ) { cout < < ( a - b - c ) < < endl ; } int main () { // for placeholders using namespace std :: placeholders ; // Use of bind() to bind the function // _1 is for first parameter and assigned // to 'a' in above declaration. // 2 is assigned to b // 3 is assigned to c auto fn1 = bind ( func _1 2 3 ); // 2 is assigned to a. // _1 is for first parameter and assigned // to 'b' in above declaration. // 3 is assigned to c. auto fn2 = bind ( func 2 _1 3 ); // calling of modified functions fn1 ( 10 ); fn2 ( 10 ); return 0 ; }
出力:
5 -11
上記のコードでは、bind() は 1 つの引数を取るように関数の呼び出しを変更し、目的の出力を返しました。
プレースホルダーのプロパティ
1. プレースホルダーの位置によって、関数呼び出しステートメント内の値の位置が決まります。
CPP // C++ code to demonstrate placeholder // property 1 #include #include // for bind() using namespace std ; // for placeholders using namespace std :: placeholders ; // Driver function to demonstrate bind() void func ( int a int b int c ) { cout < < ( a - b - c ) < < endl ; } int main () { // for placeholders using namespace std :: placeholders ; // Second parameter to fn1() is assigned // to 'a' in fun(). // 2 is assigned to 'b' in fun // First parameter to fn1() is assigned // to 'c' in fun(). auto fn1 = bind ( func _2 2 _1 ); // calling of function cout < < 'The value of function is : ' ; fn1 ( 1 13 ); // First parameter to fn2() is assigned // to 'a' in fun(). // 2 is assigned to 'b' in fun // Second parameter to fn2() is assigned // to 'c' in fun(). auto fn2 = bind ( func _1 2 _2 ); // calling of same function cout < < 'The value of function after changing' ' placeholder position is : ' ; fn2 ( 1 13 ); return 0 ; }
出力:
The value of function is : 10 The value of function after changing placeholder position is : -14
上記のコードでは、関数呼び出し内の 1 と 13 の位置が同じであっても、プレースホルダーの位置を変更すると関数の呼び出し方法が変わりました。
2. プレースホルダーの数によって、関数に渡すために必要な引数の数が決まります。
任意の番号を使用できます。関数呼び出しステートメント内のプレースホルダーの数 (明らかに引数の最大数よりも少ない)。残りの値はユーザー定義のデフォルト値に置き換えられます。
CPP // C++ code to demonstrate placeholder // property 2 #include // for bind() #include using namespace std ; // for placeholders using namespace std :: placeholders ; // Driver function to demonstrate bind() void func ( int a int b int c ) { cout < < ( a - b - c ) < < endl ; } int main () { // for placeholders using namespace std :: placeholders ; // 1 placeholder auto fn1 = bind ( func _1 2 4 ); // calling of function with 1 argument cout < < 'The value of function with 1 ' 'placeholder is : ' ; fn1 ( 10 ); // 2 placeholders auto fn2 = bind ( func _1 2 _2 ); // calling of function with 2 arguments cout < < 'The value of function with 2' ' placeholders is : ' ; fn2 ( 13 1 ); // 3 placeholders auto fn3 = bind ( func _1 _3 _2 ); // calling of function with 3 arguments cout < < 'The value of function with 3 ' 'placeholders is : ' ; fn3 ( 13 1 4 ); return 0 ; }
出力:
The value of function with 1 placeholder is : 4 The value of function with 2 placeholders is : 10 The value of function with 3 placeholders is : 8
上記のコードでは明らかに no.関数を呼び出すために必要な引数の数に等しいプレースホルダーの数。関数のバインドは、プレースホルダーの数と位置によって指示されます。