Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2004-08-30 15:03:38


Hi Mattias,

What you want to do is perfectly doable with BLL in my opinion. However I am
not sure of the exact syntax. For now, here is what you want to do using
boost::bind instead of boost::lambda::bind. The header is <boost/bind.hpp>.

std::for_each(v.begin(),
        v.end(),
      boost::bind(&Map::insert,
              &m,
            boost::bind(&std::make_pair<Map::key_type, Map::mapped_type>,
                    boost::bind(&A::a_, _1),
                  boost::bind(&A::b_, _1))));
    
   
or

std::transform(v.begin(),
        v.end(),
      std::inserter(m, m.end()),
      boost::bind(&std::make_pair<Map::key_type, Map::mapped_type>,
                boost::bind(&A::a_, _1),
            boost::bind(&A::b_, _1)));

Both will do what you want.

Cheers,

-delfin

> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Mattias Brändström
> Sent: Monday, August 30, 2004 1:34 AM
> To: boost-users_at_[hidden]
> Subject: [Boost-users] BLL and member variables
>
> Hello,
>
> I really like the BLL and would like to use it as much as possible. I
> have run into a small problem though. My code looks like this:
>
> #include <boost/lambda/bind.hpp>
> #include <boost/lambda/lambda.hpp>
>
> #include <map>
> #include <vector>
>
> class A
> {
> public:
> A(int a, int b) : a_(a), b_(b) { }
> int a_;
> int b_;
> };
>
> int main()
> {
> using namespace boost::lambda;
>
> std::vector<A> v;
> v.push_back(A(1, 2));
>
> typedef std::map<int, int> Map;
> Map m;
>
> for_each(v.begin(), v.end(),
> bind(&Map::insert,
> std::make_pair(bind(&A::a_, _1), bind(&A::b_, _1))));
>
> return 0;
> }
>
> What I want to do is to populate a map with the information from the
> vetctor v. For each element in v I want to insert a value and a key into
> the map something like this:
>
> void operator()(const A& a) const {
> m_.insert(std::make_pair(a.a_, a.b_));
> }
>
> I found some information on how to use member variables as targets in
> the BLL documentation and that is what I have tried to use in the code
> above. It won't compile however, using g++ I get the following error:
>
> g++ -c -o test.o test.cpp
> test.cpp: In function `int main()':
> test.cpp:27: error: no matching function for call to `bind(<unknown type>,
>
> std::pair<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base
> <boost::lambda::action<2,
> boost::lambda::function_action<2, boost::lambda::detail::unspecified>
> >,
> boost::tuples::tuple<int A::*, const
> boost::lambda::lambda_functor<boost::lambda::placeholder<1> >,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type> > >,
>
> boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::la
> mbda::action<2,
> boost::lambda::function_action<2, boost::lambda::detail::unspecified>
> >,
> boost::tuples::tuple<int A::*, const
> boost::lambda::lambda_functor<boost::lambda::placeholder<1> >,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type,
> boost::tuples::null_type, boost::tuples::null_type> > > >)'
> scons: *** [test.o] Error 1
> scons: building terminated because of errors.
>
>
> If someone here has any ideas of how to accomplish this it would be
> really nice!
>
> Thanks in advance!
>
> Regards,
> Mattias
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net