повысить лямбда-вопрос - PullRequest
       9

повысить лямбда-вопрос

1 голос
/ 18 августа 2010
#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 за вашу помощь!

edit: СПАСИБО, ребята!Рабочий код ниже ...

#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;
}

Ответы [ 2 ]

2 голосов
/ 18 августа 2010

Эта программа запускается и выдает ожидаемый результат после следующих изменений:

  1. реализовать Foo::operator<(const Foo&) const (в противном случае set<Foo> не скомпилируется)
  2. положить typedef set<Foo> fooset; после class Foo
  3. устраняет неоднозначность _1 между заполнителями boost.bind и boost.lambda
  4. используйте '\ n' вместо endl, как уже упоминалось.
1 голос
/ 18 августа 2010

Во-первых, не путайте boost :: bind и boost :: lambda :: bind, это разные вещи.

Измените вызов boost :: bind в цикле foreach на этот (Удалить префикс boost ::):

bind (&Foo::get_i, _1)

Затем измените endl в нижней части на '\n'.

...