Page 1 of 1

Compile Error on using STL set...

Posted: Tue Jul 25, 2006 7:22 am
by hyperion
I don't know why this gets compile error, I found out that OJ gives compile error just when I use find function on a class templated set like the code below, can anyone help me out how can I use the find function on set...

Code: Select all

#include <set>
#include <iostream>

using namespace std;

class Test {
  public :
    int x, y;
};
class comp {
  public:
    int operator () (Test a, Test b)
    {
      if (a.x < b.x || (a.x == b.x && a.y < b.y)) return 1;
      return 0;
    }
};

int main()
{
  set<Test, comp> s;
  Test temp;

  temp.x = 1; temp.y = 2;
  s.insert(temp);
  temp.x = 2; temp.y = 2;
  s.insert(temp);
  temp.x = 2; temp.y = 1;
  s.insert(temp);

  temp.x = 1;  temp.y = 2;
  set<Test, comp>::iterator i = s.find(temp);
  if ( i != s.end() ) cout << "Yes\n"; else cout << "No\n";
  return 0;
}
Thanks alot in advance..[/code]

Posted: Wed Jul 26, 2006 12:41 pm
by misof
I changed your comparison function to a more correct one:

Code: Select all

class comp {
  public:
    bool operator () (const Test &a, const Test &b) const
    {
      if (a.x < b.x || (a.x == b.x && a.y < b.y)) return true;
      return false;
    }
};
now it compiles under all versions of g++ I found

Posted: Sat Jul 29, 2006 8:58 am
by hyperion
Thanks alot misof, you are a real guru
:D