boost lambda question
#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;
}
This program runs and produces the expected output after the following changes:
- implement
Foo::operator<(const Foo&) const
(otherwiseset<Foo>
will not compile) - put
typedef set<Foo> fooset;
afterclass Foo
- disambiguate _1 between the boost.bind and boost.lambda placeholders
- use '\n' instead of endl, as already mentioned.
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 endl
s at the bottom to '\n'
.
精彩评论