2010-08-18 33 views
1
#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; 
} 

Voici mon exemple de code. Je veux for_each un ensemble de Foo et produire un ensemble d'un type d'un membre de Foo, dans ce cas un int. Je ne suis pas vraiment sûr de ce que je fais mal, mais je fais vraiment quelque chose de mal.boost question lambda

TIA pour votre aide!

edit: MERCI Guys! Code de travail est ci-dessous ...

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

Votre code ne compile pas. Est-ce le problème pour lequel vous voulez de l'aide? – zwol

Répondre

2

Ce programme fonctionne et produit le résultat attendu après les modifications suivantes:

  1. mettre en œuvre Foo::operator<(const Foo&) const (sinon set<Foo> ne compilera pas)
  2. mis typedef set<Foo> fooset; après class Foo
  3. désambiguïser _1 entre le boost.bind et boosters boost.lambda
  4. utilisez '\ n' au lieu de endl, comme déjà mentionné.
1

Tout d'abord ne pas mélanger boost :: bind et boost :: lambda :: bind, ce sont des choses différentes.

Modifier l'appel à stimuler :: bind dans la boucle de foreach à cette (Retirer le boost :: préfixe):

bind (&Foo::get_i, _1) 

changer ensuite les endl s en bas à '\n'.