Answer the question
In order to leave comments, you need to log in
Why doesn't std::greater() work with self-written structs?
Good evening.
I have a triple structure that has a greater than operator defined:
struct triple
{
int c, v, u;
triple(int cn, int vn, int un)
{
c = cn, v = vn, u = un;
}
bool operator >(triple n)
{
if (c > n.c)
return true;
return false;
}
};
int main()
{
triple a(10, 1, 2);
triple b(20, 2, 3);
priority_queue<triple, vector<triple>, greater<triple>> q;
q.push(a);
q.push(b);
return 0;
}
bool operator >(const triple& a, const triple& b)
{
return a > b;
}
bool operator >(const triple& a, const triple& b)
{
if (a.c > b.c)
return true;
return false;
}
Answer the question
In order to leave comments, you need to log in
We need const-correctness, and the absence of unnecessary copying is also important. An example of an operator defined in a class:
bool operator >(const triple& n) const
{
return c > n.c;
}
using triple = std::tuple<int, int, int>;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question