logo

כושר פונקציה ומציבי מקום ב- 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. מיקום מציין המקום קובע את מיקום הערך בהצהרת שיחת הפונקציה 

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

בקוד לעיל בבירור המס '. של מצייני המקום המשווים למספר הטיעונים הנדרשים כדי להתקשר לפונקציה. קשירת הפונקציה מכוונת על ידי מספר ומיקומם של מצייני המיקום. 

אנקפסולציה java