te********@gmail.com wrote:
Hi,
I want to create a set of cells (where it can be ints or doubles ).
The equatily condition is a.x==b.x and a.y==b.y.
I tried compiling this and it failed. Can someone help me.
#include<set>
#include<cstdio>
#include<iostream>
using namespace std;
struct cell {
int x,y;
};
cell a;
bool myeq(const cell &a,const cell &b)
{
if(a.x==b.x && a.y==b.y)
return true;
return false;
}
int main()
{
set <cell,myeqs;
s.insert(a);
return 0;
}
You've got two separate problems. Firstly, set requires a strict weak
ordering function, not an equality function. So you need:
bool myless(const cell &a,const cell &b)
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
Next, the second template parameter of set is a type, not a function. So
either make myless into a type:
struct myless
{
bool operator()(const cell &a,const cell &b) const
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
};
or use the function:
set<cell,bool(*)(const cell &a,const cell &b)s(myless);
I think you'll prefer the former. The final option is to define
operator< for cell.
Tom