0

我想从具有此类字符串作为属性的类列表中填充一组字符串(可用的公共 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 不是类或命名空间

谢谢。

4

1 回答 1

1

正确的方法是:

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;
}
于 2013-01-22T10:20:52.273 回答