增强 lambda 问题

发布于 2024-09-15 03:25:36 字数 2573 浏览 7 评论 0原文

#include <iostream>
#include <set>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost::lambda;



class Foo {
public:
    Foo(int i, const string &s) : m_i(i) , m_s(s) {}
    int get_i() const { return m_i; }
    const string &get_s() const { return m_s; }
    friend ostream & operator << (ostream &os, const Foo &f) {
        os << f.get_i() << " " << f.get_s().c_str() << endl;
        return os;
    }
private:
    int m_i;
    string m_s;
};

typedef set<Foo> fooset;
typedef set<int> intset;


int main()
{
    fooset fs;
    intset is;

    fs.insert(Foo(1, "one"));
    fs.insert(Foo(2, "two"));
    fs.insert(Foo(3, "three"));
    fs.insert(Foo(4, "four"));

    transform(fs.begin(), fs.end(), inserter(is, is.begin()), boost::bind(&Foo::get_i, _1));

    std::for_each(fs.begin(), fs.end(), cout << _1 << endl);
    std::for_each(is.begin(), is.end(), cout << _1 << endl);

    return 0;
}

这是我的代码示例。我想 for_each 一组 Foo 并生成一组 Foo 成员的类型,在本例中是一个 int。我不太确定我做错了什么,但我肯定做错了什么。

TIA 为您提供帮助!

编辑:谢谢大家!工作代码如下...

#include <iostream>
#include <set>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost::lambda;


class Foo {
public:
    Foo(int i, const string &s) : m_i(i) , m_s(s) {}
    int get_i() const { return m_i; }
    const string &get_s() const { return m_s; }
    friend ostream & operator << (ostream &os, const Foo &f) {
        os << f.get_i() << " " << f.get_s().c_str() << '\n';
        return os;
    }

private:
    int m_i;
    string m_s;
};

bool operator < (const Foo &lf, const Foo &rf) {
    return (lf.get_i() < rf.get_i()); 
}

typedef set<Foo> fooset;
typedef set<int> intset;


int main()
{
    fooset fs;
    intset is;

    fs.insert(Foo(1, "one"));
    fs.insert(Foo(2, "two"));
    fs.insert(Foo(3, "three"));
    fs.insert(Foo(4, "four"));

    transform(fs.begin(), fs.end(), inserter(is, is.begin()), boost::lambda::bind(&Foo::get_i, boost::lambda::_1));

    std::for_each(fs.begin(), fs.end(), cout << boost::lambda::_1 << '\n');
    std::for_each(is.begin(), is.end(), cout << boost::lambda::_1 << '\n');

    return 0;
}
#include <iostream>
#include <set>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost::lambda;



class Foo {
public:
    Foo(int i, const string &s) : m_i(i) , m_s(s) {}
    int get_i() const { return m_i; }
    const string &get_s() const { return m_s; }
    friend ostream & operator << (ostream &os, const Foo &f) {
        os << f.get_i() << " " << f.get_s().c_str() << endl;
        return os;
    }
private:
    int m_i;
    string m_s;
};

typedef set<Foo> fooset;
typedef set<int> intset;


int main()
{
    fooset fs;
    intset is;

    fs.insert(Foo(1, "one"));
    fs.insert(Foo(2, "two"));
    fs.insert(Foo(3, "three"));
    fs.insert(Foo(4, "four"));

    transform(fs.begin(), fs.end(), inserter(is, is.begin()), boost::bind(&Foo::get_i, _1));

    std::for_each(fs.begin(), fs.end(), cout << _1 << endl);
    std::for_each(is.begin(), is.end(), cout << _1 << endl);

    return 0;
}

Here's my code example. I want to for_each a set of Foo and produce a set of a type of a member of Foo, in this case an int. I'm not really sure what I'm doing wrong, but I am definitely doing something wrong.

TIA for your help!

edit: THANKS Guys! Working code is below...

#include <iostream>
#include <set>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost::lambda;


class Foo {
public:
    Foo(int i, const string &s) : m_i(i) , m_s(s) {}
    int get_i() const { return m_i; }
    const string &get_s() const { return m_s; }
    friend ostream & operator << (ostream &os, const Foo &f) {
        os << f.get_i() << " " << f.get_s().c_str() << '\n';
        return os;
    }

private:
    int m_i;
    string m_s;
};

bool operator < (const Foo &lf, const Foo &rf) {
    return (lf.get_i() < rf.get_i()); 
}

typedef set<Foo> fooset;
typedef set<int> intset;


int main()
{
    fooset fs;
    intset is;

    fs.insert(Foo(1, "one"));
    fs.insert(Foo(2, "two"));
    fs.insert(Foo(3, "three"));
    fs.insert(Foo(4, "four"));

    transform(fs.begin(), fs.end(), inserter(is, is.begin()), boost::lambda::bind(&Foo::get_i, boost::lambda::_1));

    std::for_each(fs.begin(), fs.end(), cout << boost::lambda::_1 << '\n');
    std::for_each(is.begin(), is.end(), cout << boost::lambda::_1 << '\n');

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2024-09-22 03:25:36

该程序在进行以下更改后运行并产生预期的输出:

  1. 实现 Foo::operator<(const Foo&) const (否则 set 将无法编译)
  2. put typedef set; fooset;class Foo 消除
  3. boost.bind 和 boost.lambda 占位符之间的 _1 歧义
  4. 时,使用 '\n' 而不是 endl,如前所述。

This program runs and produces the expected output after the following changes:

  1. implement Foo::operator<(const Foo&) const (otherwise set<Foo> will not compile)
  2. put typedef set<Foo> fooset; after class Foo
  3. disambiguate _1 between the boost.bind and boost.lambda placeholders
  4. use '\n' instead of endl, as already mentioned.
少年亿悲伤 2024-09-22 03:25:36

首先不要混淆 boost::bind 和 boost::lambda::bind,它们是不同的东西。

将 foreach 循环中对 boost::bind 的调用更改为这样(删除 boost:: 前缀):

bind (&Foo::get_i, _1)

然后将底部的 endl 更改为 '\n'

Firstly don't mix up boost::bind and boost::lambda::bind, they're different things.

Change the call to boost::bind in the foreach loop to this (Remove the boost:: prefix):

bind (&Foo::get_i, _1)

Then change the endls at the bottom to '\n'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文