Ibland måste vi manipulera funktionen av en funktion efter behovet, dvs ändra vissa argument till standard etc. standardargument begränsar mångsidigheten i en funktion och tvingar oss att använda standardargumenten och det också med liknande värden varje gång. Från C ++ 11 och framåt har introduktionen av bindningsfunktionen underlättat denna uppgift.
Hur fungerar bindande ()?
Bind Funktion med hjälp av platshållare hjälper till att manipulera positionen och antalet värden som ska användas av funktionen och modifierar funktionen enligt önskad utgång.
byt namn på katalogen på linux
Vad är platshållare?
Platshållare är namnutrymmen som riktar positionen för ett värde i en funktion. De representeras av _1 _2 _3 ...
Exempel:
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 koden ovan modifierade bindningen () samtalet för en funktion för att ta ett argument och returnerade den önskade utgången.
Platshållares egenskaper
1. Platshållarens position bestämmer värdet i funktionssamtalet
lambda funktion javaCPP
// 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:
The value of function is : 10 The value of function after changing placeholder position is : -14
I ovanstående kod även om positionen 1 och 13 var densamma i en funktion kallar förändringen i platshållarnas position hur funktionen kallades.
2. Antalet platshållare bestämmer antalet argument som krävs för att passera i funktionen.
Vi kan använda valfritt nej. av platshållare i funktionssamtalet (uppenbarligen mindre än det maximala antalet argument). REST-värdena ersätts av de användardefinierade standardvärdena.
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; }
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 ovanstående kod är det tydligt nr. av platshållare motsvarade antalet argument som krävs för att ringa funktionen. Bindningen av funktionen riktas av platsen och positionen för platshållare.
ascii-tabell i c