Use of bind in boost library

Source: Internet
Author: User
Tags types of functions



The bind of the boost library is a further generalization of STL's bind1st and bind2nd, so we have to ask, what are the advantages of bind with respect to bind1st and bind2nd? Here's a concrete example to illustrate the problem.
If there is an integer container vector that now wants to find out the number of elements in the container that are not greater than 10, then we can do it in a number of ways:
1. Free function mode
BOOL lessequaltoten (int ntemp)
{
return ntemp <= 10;
}
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), Lessequaltoten);
2. Function Object mode
struct Tlessequaltoten
{
BOOL operator () (int ntemp)
{
return ntemp <= 10;
}
}
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), Tlessequaltoten ());
3. Both of these methods are cumbersome and require the writing of separate functions, while the bind1st or bind2nd approach is relatively concise
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), bind2nd (Less_equal<int> (), 10));
Or
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), Not1 (bind2nd (greater<int> (), 10)));
4. Bind mode
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), Bind (Less_equal<int> (), _1,10));
By comparing the above 4 ways of implementation we can find that the first two are slightly more cumbersome in syntax, and the latter two are relatively concise, especially the last one, due to the use of placeholders, more in line with the logical thinking habits of people. But the comparison alone is not enough to see the advantages of BIND. So, now let's find out the number of elements in the container that are greater than 5 but less than 10, what to do? The adoption of the 3rd way has not been achieved, the first two ways can be achieved, but the shortcomings are obvious, the grammar is too concise. Let's try to implement it using bind.
int ncount = count_if (Vcttemp.begin (), Vcttemp.end (), Bind (Logical_and<bool> (), Bind (Greater<int> (), _1, 5 ), Bind (Less<int> (), _1, 10));
As you can see, because bind supports nesting, it is very concise in syntax. But bind provides much more than that, and it also provides support for calls to free functions, function objects, member functions, member variables, and virtual functions, with a fully consistent syntax. Without bind, the way we call all of these different types of functions will be very inconsistent and even messy, as summarized below.
1. bind1st and bind2nd do not support direct binding to free functions, and if you want to bind free functions, you need to first convert the free function to a function object (derived from binary_function) using Ptr_fun.
2. When converting a member function to a function object, you need to consider whether you are passing a pointer or an object, and if it is a pointer, use Mem_fun, or if it is an object, you need to use Mem_fun_ref. In addition, Mem_fun does not support smart pointers.
3. If the member function has no arguments, convert to a function object using Mem_fun or MEM_FUN_REF, or if the member function has 1 arguments, you need to use MEM_FUN1 or mem_fun1_ref; If the member function has 2 or more than 2 arguments, I'm sorry, mem_ Fun series functions will be powerless.
4. Mem_fun and Mem_fun_ref do not support the conversion of member variables.



Reprint http://www.cnblogs.com/hujian/archive/2009/06/13/1502445.html

If you have copyright issues, please contact qq:858668791

Use of bind in boost library

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.