Thread: Compare concept

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Compare concept

    Hi guys; I just passed in the tutrials on c++ regarding to sorting on basis specific pattern and I saw like thisforget from syntax error because I want to understand the concept behind)
    Code:
    Sort(array,compare)
    Which compare is:
    [code]
    Compare(first,second)
    {
    Return (first<second)
    }
    [\code]
    This means that the sort would be in ascending order..

    What's confusing me; maybe second is the first and the first is second? So all messed up; Who said that the called function send to compare function in proper way as we thought in logic of compare?
    I mean lets assume that
    Code:
    arr=[5,6]
    Maybe the function send to compare second=5 ; first=6? So all thing in my logic function of compare will be messed up


    Any help please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    Who said that the called function send to compare function in proper way as we thought in logic of compare?
    The convention says so. If someone writes a sort function that doesn't follow this convention and yet requests for a comparator of this form, then that person's sort function has a show-stopping bug.

    Quote Originally Posted by RyanC
    Maybe the function send to compare second=5 ; first=6? So all thing in my logic function of compare will be messed up
    That's true. Maybe you should find a better function to sort your items, one that doesn't have this serious bug.

    Alternatively, you could just reverse the result, but if someone could write a sort function with such a bug and not notice, what other bugs might there be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    The convention says so. If someone writes a sort function that doesn't follow this convention and yet requests for a comparator of this form, then that person's sort function has a show-stopping bug.


    That's true. Maybe you should find a better function to sort your items, one that doesn't have this serious bug.
    so if I understand you, I make my compare as my assumptions ! and search for any other sort functions that's suitable to my assumption/logic of compare that I built?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A more sensible approach would be to look at what are the requirements for the comparator of whatever generic sort function you want to use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compare, without using compare (homework)
    By Mr.777 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2011, 02:55 AM
  2. compare with chars && compare with int
    By zcrself in forum C Programming
    Replies: 1
    Last Post: 04-22-2010, 03:19 AM
  3. Concept
    By kusal in forum C Programming
    Replies: 12
    Last Post: 01-06-2007, 10:32 PM
  4. Concept help
    By Mithoric in forum Windows Programming
    Replies: 13
    Last Post: 04-18-2004, 03:05 PM

Tags for this Thread