How to use std::find/std::find_if with a vector of custom class objects?

前端 未结 6 692
故里飘歌
故里飘歌 2020-11-29 04:09

I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object incl

6条回答
  •  日久生厌
    2020-11-29 04:48

    You can use boost::bind

    std::find_if( userlist.begin(), userlist.end(),
                boost::bind( & Nick::isFound,
                             _1 ) );
    

    just implement bool Nick::isFound()

    You can also pass the criteria

    std::find_if( userlist.begin(), userlist.end(),
                  boost::bind( & Nick::compare,
                               _1,
                               nick ) );
    

    implement

    bool Nick::compare( const Nick & nick )
    {
        return this->username == nick.username;
    }
    

提交回复
热议问题