Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 included with the same username I pass in. I did a few attempts by trying to create a new Nick object for the username I want to test and overloading the == operator and then trying to use find/find_if on the object:

    std::vector<Nick> userlist;
    std::string username = "Nicholas";

if (std::find(userlist.begin(), userlist.end(), new Nick(username, false)) != userlist.end())) {
    std::cout << "found";
}

I have overloaded the == operator so comparing Nick == Nick2 should work, but the function returns error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Nick' (or there is no acceptable conversion).

Here is my Nick class for reference:

class Nick {
private:
    Nick() {
        username = interest = email = "";
                    is_op = false;
    };
public:
    std::string username;
    std::string interest;
    std::string email;
    bool is_op;

    Nick(std::string d_username, std::string d_interest, std::string d_email, bool d_is_op) {
        Nick();
        username = d_username;
        interest = d_interest;
        email = d_email;
        is_op = d_is_op;
    };
    Nick(std::string d_username, bool d_is_op) {
        Nick();
        username = d_username;
        is_op = d_is_op;
    };
    friend bool operator== (Nick &n1, Nick &n2) {
        return (n1.username == n2.username);
    };
    friend bool operator!= (Nick &n1, Nick &n2) {
        return !(n1 == n2);
    };
};
like image 620
Speed Avatar asked Aug 04 '11 09:08

Speed


3 Answers

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;
})
like image 103
mkaes Avatar answered Oct 21 '22 20:10

mkaes


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".

like image 25
Nikko Avatar answered Oct 21 '22 19:10

Nikko


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.

like image 10
Seb Holzapfel Avatar answered Oct 21 '22 20:10

Seb Holzapfel