Noen ganger må vi manipulere driften av en funksjon i henhold til behovet, dvs standard argumenter begrenser allsidigheten til en funksjon og tvinger oss til å bruke standardargumentene og det også med lignende verdier hver gang. Fra C ++ 11 og utover har introduksjonen av BIND -funksjonen gjort denne oppgaven enklere.
Hvordan fungerer bind ()?
BIND -funksjon ved hjelp av plassholdere hjelper til med å manipulere posisjonen og antall verdier som skal brukes av funksjonen og endrer funksjonen i henhold til ønsket utgang.
np.sum
Hva er plassholdere?
Plassholdere er navneområder som leder plasseringen av en verdi i en funksjon. De er representert av _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; }
Produksjon:
5 -11
I ovennevnte kodebind () modifiserte samtalen til en funksjon for å ta 1 argument og returnerte ønsket utgang.
Egenskaper til plassholdere
1. Plasseringen til plassholderen bestemmer verdiposisjonen i funksjonsanropserklæringen
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; }
Produksjon:
struct array c programmering
The value of function is : 10 The value of function after changing placeholder position is : -14
I ovennevnte kode, selv om plasseringen av 1 og 13 var den samme i en funksjon, kaller endringen i plassholderens plasseringsposisjon måten funksjonen ble kalt på.
2. Antall plassholdere bestemmer antall argumenter som kreves for å passere i funksjonen.
Vi kan bruke hvilket som helst nei. av plassholdere i funksjonsanropserklæringen (åpenbart mindre enn det maksimale antall argumenter). Restverdiene erstattes av de brukerdefinerte standardverdiene.
j e s tCPP
// 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; }
Produksjon:
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 koden ovenfor klart nei. av plassholdere likestilt med antall argumenter som kreves for å kalle funksjonen. Bindingen av funksjonen er rettet av antall og plassering av plassering.