我想从具有字符串作为属性(可用的公共(public) getter )的类列表中填充一组字符串。
我想使用 lambda 表达式和 std::for_each 来完成。
我在想类似的事情:
class Foo
{
const std::string& getMe() const;
}
...
std::list<Foo> foos; // Let's image the list is not empty
std::set<std::string> strings; // The set to be filled
using namespace boost::lambda;
std::for_each(foos.begin(), foos.end(), bind(
std::set<std::string>::insert, &strings, _1::getMe()));
但是,我在编译时遇到了这个错误:
_1 is not a class or namespace
谢谢。
最佳答案
正确的做法是:
class Foo
{
public:
const void* getMe() const
{
return this;
}
};
int main()
{
std::list<Foo> foos(10);
std::set<const void*> addresses; // The set to be filled
using boost::bind;
std::for_each(foos.begin(), foos.end(), bind(
&std::set<const void*>::insert, &addresses, bind(&Foo::getMe, _1)));
return 0;
}
关于c++ - boost::lambda : _1 不是类或命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443793/