我在这里学习如何使用 boost::lambda 。我的一个问题是关于成员函数调用。这只是一个测试,我想使用 boost::lambda 来完成此操作,因为显然有一百万种方法可以将元素从一个容器复制到另一个容器。
我有一个 list
,它有 3 个元素:
std::list<int> a;
a.push_back(2);
a.push_back(3);
a.push_back(4);
还有一个 vector
:
vector<int> b;
我正在尝试执行以下操作:对于 a 中的每个元素,将其推送回到b。这是我的镜头:
std::for_each(a.begin(), a.end(), (b ->* (&std::vector<int>::push_back))(_1) );
问题是它不接受成员函数调用,告诉:
no match for 'operator->*' in 'b ->* &std::vector >::push_back'
我尝试了一些其他方法,但它们也不起作用。
提前致谢。
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;
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.
发布评论
评论(2)
你尝试过这样的事情吗?
Did you try something like this?
我建议您使用 Boost.Phoenix,它是 C++ 中 lambda 工具的更好实现,并且已经具有 std 容器方法的惰性版本。
http://www.boost.org /doc/libs/1_46_1/libs/spirit/phoenix/doc/html/index.html
Phoenix 目前隐藏在 Spirit 内部,但计划在 1.47 中成为一等提升公民即将到期。
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.