Re: [cpp] 在标准C中用qsort来给字符串排序,怎么写compare函数啊?

李慧霸 magazine.lihuiba at 163.com
Mon Jul 10 16:06:09 CST 2006


//from MSDN
/* QSORT.C: This program reads the command-line
 * parameters and uses qsort to sort them. It
 * then displays the sorted arguments.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int compare( const void *arg1, const void *arg2 );

void main( int argc, char **argv )
{
   int i;
   /* Eliminate argv[0] from sort: */
   argv++;
   argc--;

   /* Sort remaining args using Quicksort algorithm: */
   qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

   /* Output sorted list: */
   for( i = 0; i < argc; ++i )
      printf( "%s ", argv[i] );
   printf( "\n" );
}

int compare( const void *arg1, const void *arg2 )
{
   /* Compare all of both strings: */
   return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}

------------------				 
李慧霸
2006-07-10

-------------------------------------------------------------
发件人:heron
发送日期:2006-07-10 15:39:37
收件人:Cpp
抄送:
主题:[cpp] 在标准C中用qsort来给字符串排序,怎么写compare函数啊?


很弱智的一个问题, 谢谢大家

	

        致
礼!
 				
Best Regards!
        heron
        toqz at 163.com
          2006-07-09

_______________________________________________
Cpp mailing list
Cpp at codingnow.com
http://codingnow.com/mailman/listinfo/cpp




More information about the Cpp mailing list