Thread: qsort Comparator

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    qsort Comparator

    Hi, i would like to sort an array of object by their atribute, this is my testing project, the biggest problem is in compare function

    Thanks for help


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include "string.h"
    #include <cstdlib>
    
    
    using namespace std;
    using std::sort;
    
    class A{
    public:
         int size;
    
    
    public :
         int  get(){
              return  size;
         }
    };
    
    int compare (const void *a, const void *b) {
        A *aO = *(A **)a;
        A *bO = *(A **)b;
        return ( aO.get() - bO.get());   // now i gets error request for member of get in aO which  is none-class A*  dunno why
    }
    int main()
    {
        A **aray;
        array= new A *[4];
    
    
        for(int i=0; i < 4; i++)
        {
             array[i] = NULL;
        }
    
    
        A* a= new A();
        A* b= new A();
        A* c= new A();
        A* d= new A();
        a->size=15;
    
    
        array[0]= b;
        array[1]= a;
        array[2]= c;
        array[3]= d;
        array[0]->size=12;
        array[1]->size=10;
        array[2]->size=20;
        array[3]->size=15;
    
    
        qsort (pole[0],4,sizeof(A*),compare);
    
    
        for(int i=0; i < 3; i++)
        {
           if(array[i]!= NULL)
            cout << array[i]->get()  << endl;
        }
        return 0;
    }
    Last edited by flegmik; 10-19-2012 at 04:23 PM. Reason: some flows and mistakes

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    a0 and b0 are pointers. Use the -> syntax to access members via pointers, not the . operator.

    Also, your obvious attempt at hacking around with defining a0 and b0 in the form "A *a0 = *(A **)a" is unnecessary (and, worse, means your code has undefined behaviour, since it dereferences a pointer to a type it isn't). "A *a0 = (A *)a" will suffice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, but you seem aware of std::sort, so is there any special reason why you want to use qsort? Just for experimentation?
    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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If your goal is to sort things, then just use std::sort, not qsort.
    You need only define a less-than operator:
    Code:
    bool operator < (const A *left, const A *right)
    {
        return left->get() < right->get();
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort help
    By bbray in forum C Programming
    Replies: 11
    Last Post: 09-18-2011, 01:25 PM
  2. qsort example
    By blob84 in forum C Programming
    Replies: 7
    Last Post: 10-14-2010, 03:36 AM
  3. qsort
    By dayknight in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2004, 02:06 PM
  4. qsort please help. Thanks
    By Ann Lim in forum C Programming
    Replies: 8
    Last Post: 12-12-2002, 02:20 AM
  5. qsort
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-30-2001, 05:35 PM