OneClickAC's blog

By OneClickAC, history, 7 years ago, In English

Hi , I was going through this problem on SPOJ ( , where I was basically trying to practice OOP concepts in c++ . You can see in my code ( 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..

  • Vote: I like it
  • 0
  • Vote: I do not like it

7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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, # |
  Vote: I like it 0 Vote: I do not like it

Your comparison operation is not symmetric.

Here is your code:

original code

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

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, # ^ |
      Vote: I like it +10 Vote: I do not like it

    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 won`t get time in a live contest to write that much :P ..

    And Thanks also for correcting that (ipow) thing also , I didn`t know that .