C++의 바인딩 함수 및 자리 표시자
때때로 우리는 필요에 따라 함수의 작동을 조작해야 합니다. 즉, 일부 인수를 기본값으로 변경하는 등의 작업이 필요합니다. 기본 인수 함수의 다양성을 제한하고 매번 유사한 값으로 기본 인수를 사용하도록 강요합니다. C++11부터 바인드 기능이 도입되면서 이 작업이 더 쉬워졌습니다.
바인딩()은 어떻게 작동하나요?
자리 표시자의 도움으로 바인딩 기능은 함수에서 사용할 값의 위치와 수를 조작하는 데 도움이 되며 원하는 출력에 따라 함수를 수정합니다.
자리 표시자란 무엇입니까?
자리 표시자는 함수에서 값의 위치를 지시하는 네임스페이스입니다. 그들은 다음과 같이 표현됩니다. _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
위의 코드에서 바인딩()은 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. 함수를 호출하는 데 필요한 인수 수와 동일한 자리 표시자 수입니다. 기능 바인딩은 자리 표시자의 수와 위치에 따라 결정됩니다.