谈到的remove_if到remove_not_if
-
28-09-2019 - |
题
我怎样才能扭转谓词的返回值,并移除元素返回错误,而不是真实的?
下面是我的代码:
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算符一个更好的解决方案。
解决方案
检查 not1
在<functional>
头:
headerList.remove_if( std::not1( FindName( name ) ) );
哦,这样:
if(header.Name == m_NameToFind)
{
return true;
}
return false;
请的不这样做。
return ( header.Name == m_NameToFind );
这是多的更好,不是吗?
其他提示
另外,您可以使用升压绑定,所以你不必写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 ::等于(),以避免header_has_name功能完全但在该点它变得有点难看。
不幸的是我觉得写一个NotFindName函子是你最好的选择。
不隶属于 StackOverflow