How to implement parameter binding in Category 11
Many beginners are not very clear about how to implement parameter binding in Category 11. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Disadvantages of lambda expressions
Remember why lambda expressions were introduced in the first place? When passing predicates to the algorithm, because the general function can not transfer information other than parameters, and the function object is more troublesome, so the lambda expression is introduced.
But lambda expressions also have a drawback: defining lambada expressions each time can be cumbersome when similar features are used many times. This article introduces another approach: parameter binding.
Standard library bind function
Continuing with the examples used in the lambda expression, if you want to find the first string with a length less than 2, you can use the following code:
Bool istarget (const string& s) {
Return s.size () < 2
}
Vector v {"This", "is", "a", "predicate", "."}
Auto found = find_if (v.begin (), v.end (), istarget)
Cout