由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教如何使用qsort() to sort string.
相关主题
which str_cmp code is better?请教sgi hash_map
求改进小函数如何让这个cur变量正确指向新地址
C++ STL map find does not work ???帮忙看看这几段程序有问题吗?
code questionstrcmp不用include ???
请教一个字符串比较排序的问题C库函数的实现
c++ string 一问请问如何对付error C2148问题:陣列的總大小不能超過 0x7fffffff 位元組
C++: use char * in Map问一下C语言编CGI的路径问题
nv的显卡能战胜intel的CPU么c++ question
相关话题的讨论汇总
话题: const话题: char话题: str2话题: qsort话题: str1
进入Programming版参与讨论
1 (共1页)
e******d
发帖数: 310
1
I want to sort strings using qsort(), and try the following code, but doesn
't work. Could you please help me out. Thank you.
=============================================
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int mystrcmp(const void * str1, const void * str2)
{
return strcmp( (const char*)str1, (const char*)str2 ) ;
}
int main()
{
int n = 4;
char *pstr[] = {"God", "Bed", "Act", "Cup"};
qsort(pstr, n, sizeof(char*), mystrcmp);
int i = 0;
for(i = 0;
X****r
发帖数: 3557
2
int mystrcmp(const void * str1, const void * str2)
{
return strcmp(*(const char* const*)str1, *(const char* const*)str2);
}

doesn

【在 e******d 的大作中提到】
: I want to sort strings using qsort(), and try the following code, but doesn
: 't work. Could you please help me out. Thank you.
: =============================================
: #include "stdlib.h"
: #include "stdio.h"
: #include "string.h"
: int mystrcmp(const void * str1, const void * str2)
: {
: return strcmp( (const char*)str1, (const char*)str2 ) ;
: }

z****e
发帖数: 2024
3
C还很流行吗?
还是都是C++的天下了?

【在 X****r 的大作中提到】
: int mystrcmp(const void * str1, const void * str2)
: {
: return strcmp(*(const char* const*)str1, *(const char* const*)str2);
: }
:
: doesn

p***o
发帖数: 1252
4
It really doesn't matter which language you are using.
Actually qsort is a good example to show someone familiar with C
the idea of functor in C++. While C++ automatically matches
(or finds a mismatch of) the types for std::sort, in C you have
to do it by hand and be very careful; otherwise you will get
the same error as LZ.

【在 z****e 的大作中提到】
: C还很流行吗?
: 还是都是C++的天下了?

e**********n
发帖数: 359
5
Can you just use *(char **)str1 and *(char **)str2 ?

【在 X****r 的大作中提到】
: int mystrcmp(const void * str1, const void * str2)
: {
: return strcmp(*(const char* const*)str1, *(const char* const*)str2);
: }
:
: doesn

X****r
发帖数: 3557
6
Yes, in this case they are the same in result.
But generally speaking, const should be used wherever applicable
to help prevent programming errors.

【在 e**********n 的大作中提到】
: Can you just use *(char **)str1 and *(char **)str2 ?
c*******9
发帖数: 6411
7
Xentar,
could you explain why the correct function is :
int mystrcmp(const void * str1, const void * str2)
{
return strcmp(*(const char* const*)str1, *(const char* const*)str2);
}
and not:
nt mystrcmp(const void * str1, const void * str2)
{
return strcmp( (const char*)str1, (const char*)str2 ) ;
}
The array elements here are pointer to char*?
Thanks for the help!
X****r
发帖数: 3557
8
qsort passes pointers to the objects being compared. Your objects
in the array are "char *", thus the comparison function parameters
have actual type "char **".
For example, if you're sorting integers, you would do:
int myintcmp(const void *p1, const void *p2) {
return *(const int*)p1 - *(const int *)p2;
}
int data[] = {1,3,2,4};
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), myintcmp);
Just replace "int" in above example with "char *" you'll see.

const*)str2);

【在 c*******9 的大作中提到】
: Xentar,
: could you explain why the correct function is :
: int mystrcmp(const void * str1, const void * str2)
: {
: return strcmp(*(const char* const*)str1, *(const char* const*)str2);
: }
: and not:
: nt mystrcmp(const void * str1, const void * str2)
: {
: return strcmp( (const char*)str1, (const char*)str2 ) ;

c*******9
发帖数: 6411
9
Thanks!
1 (共1页)
进入Programming版参与讨论
相关主题
c++ question请教一个字符串比较排序的问题
string /File IO processing using Cc++ string 一问
在用C写shell,遇到问题C++: use char * in Map
c: compare ampersand & symbol with a stringnv的显卡能战胜intel的CPU么
which str_cmp code is better?请教sgi hash_map
求改进小函数如何让这个cur变量正确指向新地址
C++ STL map find does not work ???帮忙看看这几段程序有问题吗?
code questionstrcmp不用include ???
相关话题的讨论汇总
话题: const话题: char话题: str2话题: qsort话题: str1