help with qsort() function.

This is a discussion on help with qsort() function. within the C++ Programming forums, part of the General Programming Boards category; can anyone help give a sample use of this function? It is found in the <stdlib.h> library. I want to ...

  1. #1
    Unregistered
    Guest

    Unhappy help with qsort() function.

    can anyone help give a sample use of this function?
    It is found in the <stdlib.h> library.

    I want to sort an array of structs in ascending order with this function.

    Code:
    #include<iostream.h>
    
    struct type{int a,b;};
    
    int main()
    {
     list<type>[10];
     for(int x=9;x>0;x--) //fill in values
     {
      list[x]=x;
     }
     //qsort function here
     for(x=0;x<10;x++) //output array in order
     {
     cout<<list[x];
     }
     return 0;
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    If you're sorting a std:list, you should use it's sorting method not qsort(). Also there's already a pre-defined template struct - pair, that already has operator < defined, so can be used in a sort. If you really want to use your own struct you'll have to write your own comparison function.

    Code:
    #include<iostream>
    #include <list>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
     list<pair<int,int> > l(10);
     list<pair<int,int> >::iterator it;
     int x=9;
     for(it=l.begin();it!=l.end();++it) //fill in values
     {
         it->first=x--;  
     }
     l.sort();
    
     for(it=l.begin();it!=l.end();++it) //output array in order
     {
     cout<<it->first;
     }
     return 0;
    }

  3. #3
    Unregistered
    Guest
    anyone know where I can learn about the qsort() function? thanx.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Doing a search on google (or these boards) will give you plenty of examples. Why do you need to use qsort .

  5. #5
    Unregistered
    Guest
    on my actual program, I have over 200,000,000 elements.
    I've tried the insertion, selection and bubble but they were time consuming.

    and each element are structs.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    qsort() doesn't sort lists. If you want to use an array based sort you should place the elements in an array(or vector) and then use std::sort(). If you want to use a list as a base data structure, you could look into the associative containers (std::map,std::set), which normally use something like a red-black tree, or use std::list::sort(). All of these will be quicker than an insertion, selection or bubble sort.

  7. #7
    Unregistered
    Guest
    I'm confused. what I'm trying to do is to sort an array of structs by a member.

    this is a sample of what I have:

    Code:
    struct type{int a,b,c};
    
    int main()
    {
    type list[2000];
    for(int i=0;i<2000;i++)
     {
      list[i].a=i+10;
      list[i].b=i+20;
      list[i].c=i+30;
     }
    
    //insert here: sort the struct array by a member
    
    qsort() ?
    ...
    }

    so if I want to sort the list with for example, member b. how do I do this?

    also, what are the parameters (and what do they represent) for the qsort() function?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >so if I want to sort the list with for example, member b. how do I do this?

    Like this -

    Code:
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    struct type{
    	int a,b,c;
    	operator <(type &f)
    	{
    		return this->b<f.b;
    	}
    	};
    
    
    int main () {
    
    	type list[2000];
    	int i=0,j=0;
    	for(i=1999,j=0;i>=0;i--,j++)
    	{
    		list[j].a=i+10;
    		list[j].b=i+20;
    		list[j].c=i+30;
    	}
    
    	sort(&list[0],&list[2000]);
    
    	for(i=0;i<2000;i++)
    	{
    		cout << list[i].b<<endl;
    	}
    
    	return 0;     
    
    }
    You'd use std::sort over qsort as it gives better performance guarantees and can inline the comparison if you write a functor.

  9. #9
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    If you want information on a specific function, why not look it up on MSDN?

  10. #10
    Unregistered
    Guest
    Sorensen, major thanks to you.

    the qsort() function I still can't fully understand yet but the algorithm library helped a lot.

    everything works now (still kinda time consuming) but how do I add an inline to the function? I mean, I only need to use that function once.

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >but how do I add an inline to the function?

    If you're just using one of the built in functors, such less or greater (the default argument for std::sort, is the less functor) then they're already declared inline. All you have to do is write operator < inline as in my example above.

  12. #12
    Unregistered
    Guest
    I hope this struct is correct... it works but want to double check.

    an error occured when I added "inline" after the "operator <" so I added it before the "operator <"

    Code:
    struct values
    {
     int w,s,i,x,c,n,q,o,z,e,axisx,axisy,axisz,
          six,cinq,onze,totala,totals,totalm,totald,end,result;
     char di[3];
     bool cur,lin,nuls;
     inline operator <(values & copy)
     {
      return this->end<copy.end;
     }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21