Nogle gange er vi nødt til at manipulere driften af en funktion i henhold til behovet, dvs. at ændre nogle argumenter til standard osv. Forefinerende en funktion til at have Standardargumenter Begrænser alsidigheden af en funktion og tvinger os til at bruge standardargumenterne og det også med lignende værdier hver gang. Fra C ++ 11 og fremover har introduktionen af bindingsfunktionen gjort denne opgave lettere.
Hvordan fungerer bind ()?
Bind funktion ved hjælp af pladsholdere hjælper med at manipulere positionen og antallet af værdier, der skal bruges af funktionen og ændrer funktionen i henhold til den ønskede output.
pointer i c
Hvad er pladsholdere?
Stedholdere er navneområder, der dirigerer placeringen af en værdi i en funktion. De er repræsenteret af _1 _2 _3 ...
Eksempel:
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; }
Produktion:
5 -11
I ovenstående kode binder Bind () opkaldet fra en funktion til at tage 1 argument og returnerede det ønskede output.
Egenskaber for pladsholdere
1. Placeringen af pladsholderen bestemmer værdipositionen i funktionsopkaldsopgørelsen
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; }
Produktion:
datatyper i java
The value of function is : 10 The value of function after changing placeholder position is : -14
I ovenstående kode, selvom placeringen af 1 og 13 var den samme i en funktion, ring ændringen i placeringen af pladsholdere ændrede den måde, funktionen blev kaldt på.
2. Antallet af pladsholdere bestemmer antallet af argumenter, der kræves for at videregive funktionen.
Vi kan bruge ethvert nr. af pladsholdere i funktionsopkaldsopgørelsen (åbenbart mindre end det maksimale antal argumenter). Restværdierne erstattes af de brugerdefinerede standardværdier.
git push-kommandoCPP
// 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; }
Produktion:
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
I ovenstående kode klart nr. af stedholdere svarede til antallet af argumenter, der kræves for at kalde funktionen. Bindingen af funktionen er instrueret af antallet og placeringen af pladsholdere.