简体   繁体   English

如何使用bind1st和bind2nd?

[英]How to use bind1st and bind2nd?

I would like to learn how to use binding functions. 我想学习如何使用绑定函数。 Here is the idea: I have this function which takes to parameters: 这是一个想法:我有这个参数的功能:

void print_i(int t, std::string separator)
{
        std::cout << t << separator;
}

And I would like to do: 我想这样做:

std::vector<int> elements;
// ...
for_each(elements.begin(), elements.end(), std::bind2nd(print_i, '\n'));

But it does not work ! 但它不起作用!

Here is what I get: 这是我得到的:

/usr/include/c++/4.3/backward/binders.h: In instantiation of ‘std::binder2nd<void ()(int, std::string)>’:
main.cpp:72:   instantiated from here
/usr/include/c++/4.3/backward/binders.h:138: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:141: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:145: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:149: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:155: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:140: error: field ‘std::binder2nd<void ()(int, std::string)>::op’ invalidly declared function type
/usr/include/c++/4.3/backward/binders.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = void ()(int, std::string), _Tp = char]’:
main.cpp:72:   instantiated from here
/usr/include/c++/4.3/backward/binders.h:164: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Funct = std::binder2nd<void ()(int, std::string)>]’:
main.cpp:72:   instantiated from here
/usr/include/c++/4.3/bits/stl_algo.h:3791: error: no match for call to ‘(std::binder2nd<void ()(int, std::string)>) (int&)’
make: *** [all] Error 1

I could use functor, but it is quicker to use binding. 我可以使用仿函数,但使用绑定更快。

Thanks! 谢谢!

You need to use a Copyable/Refrencable object, the following works: 您需要使用Copyable / Refrencable对象,以下工作:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

void print_i(int t, std::string separator)
{
   std::cout << t << separator;
}

int main()
{
   std::vector<int> elements;
   std::string delim = "\n";
   for_each(elements.begin(), 
            elements.end(),
            std::bind2nd(std::ptr_fun(&print_i),delim));
   return 0;
}

Normally you can get the same effect by simply doing the following: 通常只需执行以下操作即可获得相同的效果:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
   std::vector<int> elements;
   std::copy(elements.begin(),
             elements.end(),
             std::ostream_iterator<int>(std::cout,"\n"));
   return 0;
}

Also assuming you have access to TR1 in the STL you're using, its always best to revise/replace any uses of bind1st and bind2nd with std::bind 假设你在你正在使用的STL中访问TR1,它总是最好修改/替换bind1st和bind2nd与std :: bind的任何用法

The argument to bind2nd must be an AdaptableBinaryFunction . bind2nd的参数必须是AdaptableBinaryFunction A plain binary function does not fulfill this requirement (an adaptable function required typedefs for its return and argument types, a plain function type does not provide any typedefs). 普通的二进制函数不满足此要求(自适应函数需要typedef用于其返回和参数类型,普通函数类型不提供任何typedef)。 You could use std::bind which is probably the better choice anyway. 您可以使用std::bind ,这可能是更好的选择。

You need to do the following steps: 您需要执行以下步骤:
1. create a struct (or class) that inherits from std::binary_function 1.创建一个继承自std :: binary_function的结构(或类)
2. define your predicate function in the operator() member function of the struct created in step 1 2.在步骤1中创建的struct的operator()成员函数中定义谓词函数
3. use bind2nd to bind an appropriate value to the struct created in step 1 3.使用bind2nd将适当的值绑定到步骤1中创建的结构

I have done all this in an example. 我在一个例子中完成了所有这些。 You can read the article and download the complete code on the following link: bind and find 您可以阅读文章并在以下链接下载完整代码: bind和find

These functions are deprecated since C++11 and removed in C++17. 这些函数自C ++ 11以来已被弃用,并在C ++ 17中被删除。 As mentioned in one comment above, the better solution now is to use std::bind and the placeholders: 如上面的一条评论所述,现在更好的解决方案是使用std::bind和占位符:

void callable(int a, int b);
auto f = std::bind1st(&callable, 42); // returns a 1-arg function

becomes: 变为:

// returns a 1-arg function
auto f = std::bind(&callable, 42, std::placeholders::_1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM