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

前端 未结 6 665
故里飘歌
故里飘歌 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:30

    If you are using C++0X you can use a simple lambda expression

    std::string username = "Nicholas";    
    std::find_if(userlist.begin(), userlist.end(), [username](Nick const& n){
        return n.username == username;
    })
    
    0 讨论(0)
  • 2020-11-29 04:39

    You have to define operator== with two Objects outside your class, as a tool function, not a member.

    Then to make it friend just put the declaration of the function inside the class.

    try something like this:

    class Nick {
    
    public:
        friend bool operator== ( const Nick &n1, const Nick &n2);
    };
    
    
    bool operator== ( const Nick &n1, const Nick &n2) 
    {
            return n1.username == n2.username;
    }
    

    Also your find should look like this:

    std::find(userlist.begin(), userlist.end(), Nick(username, false) );
    

    No need of "new".

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-29 04:49

    I am noticing you are trying to call one constructor from another in this manner:

    Nick(std::string d_username, bool d_is_op) {
            Nick();
     ...
    

    Well, sorry, but this doesn't work. The line Nick() just creates a temporary and doesn't affect this. Constructor forwarding is only possible in C++0x (the upcoming standard)

    As to your problem - this question asked a couple of days ago about binary_search covers the same grounds. The top answer is just awesome.

    Mystical restriction on std::binary_search

    HTH.

    P.S. Ideally this should have been a comment, but it's just too verbose

    0 讨论(0)
  • 2020-11-29 04:50

    I know that you wanted to overload the == operator, but the same thing can easily be done with a predicate:

    struct UsernameIs {
        UsernameIs( string s ) : toFind(s) { }
        bool operator() (const Nick &n)
            { return n.username == toFind; }
        string toFind;
    };
    
    int main()
    {
        vector<Nick> vn(10);
        string nameToFind = "something";
        find_if(vn.begin(), vn.end(), UsernameIs(nameToFind));
    }
    

    Note that in C++0x, you can do the same thing with a lambda expression much more concisely.

    0 讨论(0)
  • 2020-11-29 04:54

    You are passing a pointer to the find function. Drop the new:

    std::find(userlist.begin(), userlist.end(), Nick(username, false))
    

    Also, your operators should accept their arguments by const reference, they don't modify them.

    bool operator== (const Nick &n1, const Nick &n2)
    
    0 讨论(0)
提交回复
热议问题