c++ - Boost::bind 和 std::copy

标签 c++ boost bind

我正在尝试使用 Boost::bind 和 std::copy 来打印列表列表中的值。显然,我可以使用循环,为了清楚起见,我最终可能会这样做,但我仍然想知道我在这里做错了什么。

这是我的代码的提炼版本:

#include <boost/bind.hpp>
#include <iterator>
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
using namespace boost;

int main(int argc, char **argv){
list<int> a;
a.push_back(1);

list< list<int> > a_list;
a_list.push_back(a);

ostream_iterator<int> int_output(cout,"\n");

for_each(a_list.begin(),a_list.end(),
  bind(copy,
    bind<list<int>::iterator>(&list<int>::begin,_1),
    bind<list<int>::iterator>(&list<int>::end,_1),
    ref(int_output)
  ) //compiler error at this line
);
return 0;

编译错误开始了

error: no matching function call to bind(<unresolved overloaded function type> .....

我认为这意味着绑定(bind)无法确定最外层绑定(bind)的返回类型应该是什么。我不怪它,因为我也做不到。有什么想法吗?

最佳答案

std::copy 的模板参数不能在绑定(bind)调用的上下文中推导出来。您需要明确指定它们:

copy< list<int>::iterator, ostream_iterator<int> >

还有当你写的时候:

for_each(a_list.begin().a_list.end(),

我认为你的意思是:

for_each(a_list.begin(),a_list.end(),

而你错过了#include <iostream> std::cout 的定义.

关于c++ - Boost::bind 和 std::copy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1278445/

相关文章:

c++ - 什么时候分配和初始化变量?

c++ - 为什么 std::function 不能相等比较?

c++ - 使用 boost serialize 的 Microsoft GUID 序列化?

c++ - 将指针传递给期望引用的函数会更改指向的值

c++ - 如何使用 tr1 函数并绑定(bind)参数不断变化的函数?

c - 使用 setuid 降低到较低权限级别的正确方法是什么?

C++ 在 lambda 函数中使用此指针和绑定(bind)

c++ - 将二维数组从 C++ 传递到 qml

c# - GroupBox header 绑定(bind)中的 StringFormat 似乎不起作用

c++ - move 类的构造函数