如何使用bind1st和bind2nd?

时间:2009-09-13 20:26:41

标签: c++ stl binding

我想学习如何使用绑定功能。 这是一个想法: 我有这个函数,它接受参数:

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

我想这样做:

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

但它不起作用!

这是我得到的:

/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

我可以使用仿函数,但使用绑定会更快。

谢谢!

4 个答案:

答案 0 :(得分:26)

您需要使用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;
}

通常只需执行以下操作即可获得相同的效果:

#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;
}

另外假设你可以访问你正在使用的STL中的TR1,它总是最好修改/替换bind1st和bind2nd与std :: bind

的任何用法

答案 1 :(得分:11)

bind2nd的参数必须是AdaptableBinaryFunction。普通的二进制函数不满足此要求(自适应函数需要typedef用于其返回和参数类型,普通函数类型不提供任何typedef)。您可以使用std::bind,这可能是更好的选择。

答案 2 :(得分:3)

您需要执行以下步骤:
1。创建一个继承自std :: binary_function的结构(或类)
2。在步骤1中创建的struct的operator()成员函数中定义谓词函数
3。使用bind2nd将适当的值绑定到在步骤1中创建的结构

我在一个例子中完成了所有这些。您可以阅读文章并在以下链接下载完整代码:bind and find

答案 3 :(得分:1)

从C ++ 11开始不推荐使用这些函数,并在C ++ 17中将其删除。如上面的评论中所述,更好的解决方案是使用std::bind和占位符:

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

成为:

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