我正在尝试使用 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/