32

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);
    };
};
Speed
  • 1,424
  • 1
  • 16
  • 24

7 Answers7

51

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;
})
mkaes
  • 13,781
  • 10
  • 52
  • 72
  • 15
    @Nikko he also doesn't say that he isn't. Thus the word "if" at the beginning of this answer. – Michael Warner Nov 07 '15 at 02:12
  • Yes this all made some kind of sense in 2011 and maybe there was an edit after my comment... – Nikko Nov 07 '15 at 05:17
  • Thanks for the great answer @mkaes, i already had some understanding of using lambda functions this way but just needed to clarify it, IMHO i think this should have been the accepted answer as it does not require overloading a operator, which should only be done when necessary as it will cause a messy API if used excessively, so thanks a ton for your answer, helped alot. (= – daniel Nov 21 '16 at 06:26
22

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

Nikko
  • 4,182
  • 1
  • 26
  • 44
  • Actually, instead of defining a friend function you should just use member function bool operator==(const Nick& a). – Igor Levicki Feb 12 '15 at 13:12
  • 1
    You have the choice but there a lot of programmers who like to avoid member if not needed and to have operator like == as external free friend functions. I think it's also the case of the standard library implementations. – Nikko Feb 12 '15 at 14:27
  • Just tried as you suggested. Shouldn't the answer look like this: `std::find(userlist.begin(), userlist.end(), &Nick(username, false) );`? – Marschal Mar 25 '15 at 14:39
  • 2
    @Nikko - What is the advantage of an "external free friend function" which can work only on a single class? – Igor Levicki Aug 23 '15 at 10:26
10

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.

Seb Holzapfel
  • 3,793
  • 1
  • 19
  • 22
3

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)
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

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

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

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;
}
Nelstaar
  • 769
  • 9
  • 26
0

This works for me:

Nick.h

#include <string>

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

    bool operator==(const Nick& refNick) const
    {
        if (username != refNick.username)
            return false;
        if (interest != refNick.interest)
            return false;
        if (email != refNick.email)
            return false;
        if (is_op != refNick.is_op)
            return false;
        return true;
    }

    bool operator!=(const Nick& refNick) const
    {
        if (username == refNick.username)
            return true;
        if (interest == refNick.interest)
            return true;
        if (email == refNick.email)
            return true;
        if (is_op == refNick.is_op)
            return true;
        return false;
    }
};

main.cpp

#include <iostream>
#include <string>
#include <vector>
#include "Nick.h"

int main()
{
    std::vector<Nick> userlist;
    std::string username = "Nicholas";    
    Nick Nicholas(username, false);
    Nick John("John", true);        
    userlist.push_back(Nicholas);

    std::vector<Nick>::iterator it;

    it = std::find(userlist.begin(), userlist.end(), Nick("Nicholas", false));
    if(it != userlist.end())
        std::cout << "\n" << Nicholas.username << " was found.";
    else
        std::cout << "\n" << Nicholas.username << " was not found.";

    it = std::find(userlist.begin(), userlist.end(), John);
    if (it != userlist.end())
        std::cout << "\n" << John.username << " was found.";
    else
        std::cout << "\n" << John.username << " was not found.";
}

Result

Nicholas was found.
John was not found.
jayvatar
  • 199
  • 2
  • 9