c++ - Is a 2D integer coordinate in a set? -


i have list of 2d integer coordinates (coordinate pairs). i'd read these in and, later, determine if point has been read.

the integer data may fall anywhere within range offered integer data type, number of actual data points small. therefore, using 2d array track points have been read impractical. set seems way of doing this.

my current code accomplish follows:

#include <set> #include <iostream> using namespace std;  class grid_cell{   public:     int x,y;     grid_cell(int x, int y) : x(x), y(y) {}     grid_cell(){}     bool operator< (const grid_cell& a) const { return y<a.y || x<a.x; } };  int main(){     set<grid_cell> bob;      bob.insert(grid_cell(1,1));     bob.insert(grid_cell(-1,1));     bob.insert(grid_cell(1,-1));     bob.insert(grid_cell(-1,-1));     cout<<bob.count(grid_cell(1,1))<<endl;     cout<<bob.count(grid_cell(-1,1))<<endl;     cout<<bob.count(grid_cell(1,-1))<<endl;     cout<<bob.count(grid_cell(-1,-1))<<endl; } 

but know wrong because have inserted cells set later tells me doesn't know about. output of above should true, but, instead, follows.

1 1 0 <-????? 1 

i suspect comparator problem, not sure how fix it.

any thoughts?

the comparison indeed wrong. not satisfy required semantics. try this:

bool operator< (const grid_cell& a) const { return x<a.x || (x==a.x && y<a.y); } 

here article on subject:

http://www.drdobbs.com/cpp/a-strategy-for-defining-order-relations/240147625


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -