增强 lambda 问题
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序在进行以下更改后运行并产生预期的输出:
Foo::operator<(const Foo&) const
(否则set
将无法编译)typedef set; fooset;
在class Foo
消除This program runs and produces the expected output after the following changes:
Foo::operator<(const Foo&) const
(otherwiseset<Foo>
will not compile)typedef set<Foo> fooset;
afterclass Foo
首先不要混淆 boost::bind 和 boost::lambda::bind,它们是不同的东西。
将 foreach 循环中对 boost::bind 的调用更改为这样(删除 boost:: 前缀):
然后将底部的
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):
Then change the
endl
s at the bottom to'\n'
.