开发者

Using boost::lambda to copy a container

I'm here learning how to use boost::lambda. One question I have is about member function calling. It's just a test, and I'd like to do this with boost::lambda, as there are, obviously, half a million ways to copy the elements from one container to another container.

I have a list<int> which has 3 elements:

std::list<int> a开发者_如何学Python;
a.push_back(2);
a.push_back(3);
a.push_back(4);

And a vector<int>:

vector<int> b;

I'm trying to do the following: for each element in a, push it back in b. Here's my shot:

std::for_each(a.begin(), a.end(), (b ->* (&std::vector<int>::push_back))(_1) );

The problem is that it's not accepting the member function call, telling:

no match for ‘operator->*’ in ‘b ->* &std::vector<int, std::allocator<int> >::push_back’

I tried some other ways, but they didn't work either.

Thanks in advance.


Did you try something like this?

for_each(a.begin(), a.end(), bind(&std::vector<int>::push_back, &b, _1));


I would advise you to use Boost.Phoenix with is a better implementation of lambda facilities in C++ and already has lazy version of std containers methods.

http://www.boost.org/doc/libs/1_46_1/libs/spirit/phoenix/doc/html/index.html

Phoenix is currently hidden inside Spirit but it is planned to become a first class boost citizen in 1.47 which is due soon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜