TechNeko
TechNeko

Reputation: 35

c++ vector comparison with user defined classes? (==, <, >)

trying to compare two vectors of a user defined class, like so:

#include <vector>
using namespace std;
struct ExampleClass {
    bool operator==(ExampleClass right) {
        return true;
    }
};
int main() {
    if (vector<ExampleClass>() == vector<ExampleClass>())
        return 0;
    else
        return 1;
}

I can't seem to get it to work just using the < operator of the user defined class; I can however get it to work by writing a custom operator to compare between two vectors of said class, but I'm unclear as to whether this is what must be done or if I'm just misunderstanding the vector comparison operators. I'd also like to have it work with < and > if possible. I could just write one template operator for the comparison of two vectors to have them compare each element - but this all seems like a workaround for me not understanding something. Please explain to me what I'm doing wrong :)

Upvotes: 1

Views: 1009

Answers (1)

user10356313
user10356313

Reputation:

I think you are looking for something like this:

#include <vector>

using namespace std;

struct ExampleClass {
    bool operator==(ExampleClass right) const {
        return true;
    }
};

int main() {
    return (vector<ExampleClass>() != vector<ExampleClass>())
}

Upvotes: 1

Related Questions