如何反转谓词的返回值,并删除返回false而不是true的元素?
下面是我的代码:
headerList.remove_if(FindName(name));(请忽略缺少擦除)
使用FindName作为一个简单的函数器:
struct FindName
{
CString m_NameToFind;
FindInspectionNames(const CString &nameToFind)
{
m_NameToFind = nameToFind;
}
bool operator()(const CHeader &header)
{
if(header.Name == m_NameToFind)
{
return true;
}
return false;
}
};我想要这样的东西:
list.remove_if(FindName(name) == false);还没有使用c++0x,所以不幸的是lambda是不被允许的。我希望有比编写NotFindName函数器更好的解决方案。
发布于 2010-10-05 22:31:13
检查<functional>报头中的not1:
headerList.remove_if( std::not1( FindName( name ) ) );哦,还有这个:
if(header.Name == m_NameToFind)
{
return true;
}
return false;请不要那样做。
return ( header.Name == m_NameToFind );这样好多了,不是吗?
发布于 2010-10-05 23:42:08
或者,您可以使用boost绑定,这样就不必编写unary_function结构:
bool header_has_name (const CHeader& h, const CString& n) {return h.name == n;}
headerList.remove_if (boost::bind (header_has_name, _1, "name"));对于remove_if_not:
headerList.remove_if (!boost::bind (header_has_name, _1, "name"));您甚至可以使用std::equal()来完全避免header_has_name函数,但在这一点上,它变得有点丑陋。
发布于 2010-10-05 22:29:23
不幸的是,我认为编写一个NotFindName函数器是最好的选择。
https://stackoverflow.com/questions/3864627
复制相似问题