C ++ collection -- bind function binding, collection -- bind function binding

Source: Internet
Author: User

C ++ collection -- bind function binding, collection -- bind function binding

C ++ collection-bind function binding

Preface

Bind a function is used to bind a list of parameters to known functions to form a new function. This method of changing the existing function call mode is called function binding. It should be noted that the bind is the function adapter.

Bind Functions

Instance

# Include <iostream> # include <functional> using namespace std; using namespace std: placeholders; int main () {auto fun = [] (int * array, int n, int num) {for (int I = 0; I <n; I ++) {if (array [I]> num) cout <array [I] <ends;} cout <endl ;}; int array [] = {1, 3, 5, 7, 9 }; // _ 1, _ 2 is the placeholder auto fun1 = bind (fun, _ 1, _ 2, 5); // It is equivalent to calling fun (array, sizeof (array) /sizeof (* array), 5); fun1 (array, sizeof (array)/sizeof (* array); cin. get (); return 0 ;}
Run

7 9


The instance is simple and you can see it at a glance. However, it is worth noting that:

1. What is an adapter?

An adapter is a mechanism that modifies existing things and limits them to adapt them to new logic. It should be noted that containers, iterators, and functions all have adapters.

Bind is a function adapter that accepts a callable object and generates a new callable object to adapt to the parameter list of the original object.

The general form of bind:

  • Auto newfun = bind (fun, arg_list );
Fun is a function, and arg_list is a list of parameters separated by commas. Call newfun () and newfun will call fun (arg_list );
Common bind usage 1
In this example, the fun () call requires three parameters, and bind () requires only two parameters, because the third parameter is fixed during binding. Reduce the call of function parameters, which is the most common usage of bind.
2. _ 1, _ 2 is a placeholder defined in the namespace placeholders. _ 1 is the first parameter of newfun, _ 2 is the second parameter of newfun, and so on.
Common bind usage 2Another common usage of bind is to change the call sequence of parameters. For example, int fun (int a, int B); auto newfun = bind (fun, _ 2, _ 1); Call newfun (1, 2); call fun (2, 1 );

By the way, what is a placeholder parameter?

int fun(int a, int b, int){return a + b;}
The call method is similar to fun (1, 2, 0 );

This fun function is designed to be interesting. It accepts three int-type parameters, but the third parameter does not provide the variable name, so that this parameter cannot be used in the function body, since it has already been designed, why not? What does this mean?

This is the placeholder parameter, which provides reserved interfaces for function upgrade.



Directory of all contents

  • Directory



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.