Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning remove_if into remove_not_if

How can I reverse a predicate's return value, and remove the elements that return false instead of true?

Here is my code:

headerList.remove_if(FindName(name));

(please ignore lack of erase)

with FindName a simple functor:

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;
    }
};

I would like something like:

list.remove_if(FindName(name) == false);

Not yet using c++0x, so lambdas not allowed, sadly. I'm hoping there is a nicer solution than writing a NotFindName functor.

like image 925
DanDan Avatar asked Oct 05 '10 14:10

DanDan


1 Answers

Check not1 in the <functional> header:

headerList.remove_if( std::not1( FindName( name ) ) );

Oh, and this:

if(header.Name == m_NameToFind)
{
    return true;
}

return false;

Please don't do that.

return ( header.Name == m_NameToFind );

That's much better, isn't it?

like image 66
DevSolar Avatar answered Oct 14 '22 15:10

DevSolar