### OneClickAC's blog

By OneClickAC, history, 7 years ago,

Hi , I was going through this problem on SPOJ (http://www.spoj.com/problems/NSTEPS/) , where I was basically trying to practice OOP concepts in c++ . You can see in my code (http://pastebin.com/qubfkgEr) that I defined a point object and also defined a comparing operator for it but when i was running the code on sample tests , find operator in map is not working properly or basically I should say that since I am a newbie to these advanced features of C++ , I am , may be not using comparator function properly . Can anyone help me out to sort this problem .??

If possible , Please also tell me how to tackle this problem in java if some time I encounter it there..

• 0

 » 7 years ago, # | ← Rev. 2 →   0 Some people might also suggest another method for the problem but I would rather request everybody to just tell me about how to solve the above issue as more than just solving the problem , I want to learn one new method which will be more beneficial . :)
 » 7 years ago, # |   0 Your comparison operation is not symmetric.Here is your code: original code bool operator()(const Point& a,const Point& b) { if(a.getfirst()>b.getfirst()) { return true; } else { if(a.getsecond()>b.getsecond()) { return true; } else { return false; } } } See, if a.x > b.x, you return true. Then, by symmetry, if a.x < b.x, the function should return false. But it compares the y part instead.You can insert the missing a.x < b.x case and check if it's any better, like this: modified code bool operator()(const Point& a,const Point& b) { if(a.getfirst()>b.getfirst()) { return true; } else if(a.getfirst()b.getsecond()) { return true; } else { return false; } } } On a side note, does a.getfirst(), a.getsecond() make much sense here? The whole "private individual coordinates with obscurely named getters and setters" looks like overengineering to me. Perhaps naming the fields x and y and accessing them directly would be more, well, to the point.And on another side note, your template's #define ipow(a,b) (int)pow(a,b) is dangerous. Half of possible compilers will give you 52 = 24 with it. You might want to add something (for example, 0.5), and only then convert to an integer.
•  » » 7 years ago, # ^ |   +10 It is working and I have also understood what was the problem . Thanks for the help Gassa and yes , you are right that using x, y will be quite easy and to the point but as I said also , I am a beginner and when I started learning about OOP concepts , I read a lot about using access modifiers which is necessary for software engineering , so while practising on websites I usually try to stick to those rules so that becomes a habit . Obviously , I wont get time in a live contest to write that much :P ..And Thanks also for correcting that (ipow) thing also , I didnt know that .