我有一个 Visual Studio 2008 C++ 应用程序,我想用 boost::phoenix lambda 表达式替换一元仿函数。
就我而言,我有包含字符串的对象列表。我想删除与指定字符串不匹配的所有对象。所以,我使用这样的算法:
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
我需要做什么才能使 boost:phoenix:actor 看起来像一个Foo
对象?