更换元仿函数我有一个Visual Studio 2008的C++应用程序,我想用一个boost ::凤凰lambda表达式来代替一元仿函数。用一个boost ::凤演员
在我的情况,我有包含字符串对象的列表。我想删除所有不符合指定字符串的对象。所以,我使用这样的算法:
struct Foo
{
std::string my_type;
};
struct NotMatchType
{
NotMatchType(const std::string& t) : t_(t) { };
bool operator()(const Foo& f) const
{
return f.my_type.compare(t_) != 0;
};
std::string t_;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<Foo> list_of_foo;
/*populate with objects*/
std::string some_type = "some type";
list_of_foo.erase(
std::remove_if(list_of_foo.begin(),
list_of_foo.end(),
NotMatchType(some_type)),
list_of_foo.end());
return 0;
}
这工作正常。但是,我想清理我的代码位和摆脱NotMatchType
函子和替换它用这样一个简单的lambda表达式:
using boost::phoenix::arg_names::arg1;
list_of_foo.erase(
std::remove_if(list_of_foo.begin(),
list_of_foo.end(),
arg1.my_type.compare(some_type) != 0),
list_of_foo.end());
很明显,这是行不通的。
我也曾尝试:(arg1->*&Foo::my_type).compare(some_type) != 0
,我需要做的,使升压什么:凤:演员看起来像一个Foo
对象?
'arg1'不表示指针类型,因此,使用操作者' - > *'上它是形成不良的。 '(&arg1) - > *&Foo :: my_type!= ref(some_type)'应该可以工作。 – ildjarn
lambda表达式(即11 C++的一部分)是可悲的是,无法在Visual Studio 2008中 – PaulH
@ildjarn我不明白。 'operator - > *'过载;我已经显示的代码在我的结尾编译。 –