Boost :: bind and std :: copy

I am trying to use Boost :: bind and std :: copy to print the values ​​in a list of lists. Obviously, I could use loops, and I can do this for clarity, but I would still like to know what I'm doing wrong here.

Here is a distilled version of my code:

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

}

Compiler error

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

I think this means that bind cannot figure out what type of return for external binding should be. I do not blame it because I can’t. Any ideas?

+3
source share
2 answers

Template arguments std::copycannot be displayed in the context of a binding call. You must specify them explicitly:

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

Also when you write:

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

I think you mean:

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

#include <iostream> std::cout.

+6

, , , Boost:: bind :

struct print_int_list : public unary_function<list<int>, void>
{
   void operator()(list<int> b)
   { 
     copy(b.begin(),b.end(),ostream_iterator<int>(cout,"\n"));
   }
};

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

Boost:: bind , , .

+3

Source: https://habr.com/ru/post/1715178/


All Articles