C++;未排序指针的qsort数组 我试图在C++中按字母顺序对一个充满可变长度记录的缓冲区进行排序。我之前问过如何实现这一点,并被告知对指向记录的指针数组进行排序。我设置了一个指针数组,但意识到每个指针都指向记录的开头,但无法知道记录何时停止。因此,当我试图打印出数组中每个指针指向的记录时,对于每个指针,我得到所有记录的整个缓冲区,从指向的记录开始。(例如,如果缓冲区包含“Helloworld”,并且每个字母上都有一个指针,那么打印指针数组将产生“HelloWorldElloWorldLoWorldLoWorldDLDD”。)显然,这不是我想要的;此外,qsort似乎也不适用于指针数组。调试时,指针指向的内存空间似乎包含非常奇怪的字符,这些字符肯定不是ascii字符集的一部分,也没有包含在输入文件中。我很困惑。下面是我的代码;我如何才能做到这一点而不得到我现在得到的奇怪结果?非常感谢你,bsg int _tmain(int argc, _TCHAR* argv[]) { //allocate memory for the buffer buff = (unsigned char *) malloc(2048); realbuff = (unsigned char *) malloc(NUM_RECORDS * RECORD_SIZE); fp = fopen("postings0.txt", "r"); if(fp) { fread(buff, 1, 2048, fp); /*for(int i=0; i <30; i++) cout << buff[i] <<endl;*/ int y=0; //create a pointer to an array of unsigned char pointers unsigned char *pointerarray[NUM_RECORDS]; //point the first pointer in the pointer array to the first record in the buffer pointerarray[0] = &buff[0]; int recordcounter = 1; //iterate through each character in the buffer; //if the character is a line feed (denoting a new record), // point the next pointer in the pointer array to the next //character in the buffer (that is, the start of the next record) for(int i=0;i <2048; i++) { if(buff[i] == char(10)) { pointerarray[recordcounter] = &buff[i+1]; recordcounter++; } } //the actual qsort (NUM_RECORDS is a constant declared above; omitted here) qsort(pointerarray, NUM_RECORDS, sizeof(char*), comparator); } else cout << "sorry"; cout << sizeof(pointerarray)/sizeof(char*); for(int k=0; k < sizeof(pointerarray)/sizeof(char*);k++) { cout << pointerarray[k]; } int comparator(const void * elem1, const void * elem2) { //iterate through the length of the first string while(*firstString != char(10)) { return(strcmp(firstString, secondString)); firstString++; secondString++; / } return 0; } int-tmain(int-argc,_-TCHAR*argv[] { //为缓冲区分配内存 buff=(无符号字符*)malloc(2048); realbuff=(无符号字符*)malloc(NUM_记录*记录大小); fp=fopen(“postings0.txt”,“r”); if(fp) { fread(buff,12048,fp); /*对于(inti=0;i

C++;未排序指针的qsort数组 我试图在C++中按字母顺序对一个充满可变长度记录的缓冲区进行排序。我之前问过如何实现这一点,并被告知对指向记录的指针数组进行排序。我设置了一个指针数组,但意识到每个指针都指向记录的开头,但无法知道记录何时停止。因此,当我试图打印出数组中每个指针指向的记录时,对于每个指针,我得到所有记录的整个缓冲区,从指向的记录开始。(例如,如果缓冲区包含“Helloworld”,并且每个字母上都有一个指针,那么打印指针数组将产生“HelloWorldElloWorldLoWorldLoWorldDLDD”。)显然,这不是我想要的;此外,qsort似乎也不适用于指针数组。调试时,指针指向的内存空间似乎包含非常奇怪的字符,这些字符肯定不是ascii字符集的一部分,也没有包含在输入文件中。我很困惑。下面是我的代码;我如何才能做到这一点而不得到我现在得到的奇怪结果?非常感谢你,bsg int _tmain(int argc, _TCHAR* argv[]) { //allocate memory for the buffer buff = (unsigned char *) malloc(2048); realbuff = (unsigned char *) malloc(NUM_RECORDS * RECORD_SIZE); fp = fopen("postings0.txt", "r"); if(fp) { fread(buff, 1, 2048, fp); /*for(int i=0; i <30; i++) cout << buff[i] <<endl;*/ int y=0; //create a pointer to an array of unsigned char pointers unsigned char *pointerarray[NUM_RECORDS]; //point the first pointer in the pointer array to the first record in the buffer pointerarray[0] = &buff[0]; int recordcounter = 1; //iterate through each character in the buffer; //if the character is a line feed (denoting a new record), // point the next pointer in the pointer array to the next //character in the buffer (that is, the start of the next record) for(int i=0;i <2048; i++) { if(buff[i] == char(10)) { pointerarray[recordcounter] = &buff[i+1]; recordcounter++; } } //the actual qsort (NUM_RECORDS is a constant declared above; omitted here) qsort(pointerarray, NUM_RECORDS, sizeof(char*), comparator); } else cout << "sorry"; cout << sizeof(pointerarray)/sizeof(char*); for(int k=0; k < sizeof(pointerarray)/sizeof(char*);k++) { cout << pointerarray[k]; } int comparator(const void * elem1, const void * elem2) { //iterate through the length of the first string while(*firstString != char(10)) { return(strcmp(firstString, secondString)); firstString++; secondString++; / } return 0; } int-tmain(int-argc,_-TCHAR*argv[] { //为缓冲区分配内存 buff=(无符号字符*)malloc(2048); realbuff=(无符号字符*)malloc(NUM_记录*记录大小); fp=fopen(“postings0.txt”,“r”); if(fp) { fread(buff,12048,fp); /*对于(inti=0;i,c++,pointers,comparator,variable-length,qsort,C++,Pointers,Comparator,Variable Length,Qsort,我猜问题出在您的comparator函数中(它没有按照发布的方式编译)。 qsort提供指向比较器函数的数组元素的指针。在您的情况下,这将是指向数组中存储的char*的指针 qsort的手册页给出了以下示例: static int cmpstringp(常数无效*p1,常数无效*p2) { /*此函数的实际参数是“指向 指向char的指针”,但strcmp(3)参数是“指针” 到char”,因此下面的cast加上解引用*/ 返回strcmp(*(字符*常量*)p1,*(字符*常量*)p2); }

我猜问题出在您的comparator函数中(它没有按照发布的方式编译)。
qsort
提供指向比较器函数的数组元素的指针。在您的情况下,这将是指向数组中存储的
char*
的指针

qsort
的手册页给出了以下示例:

static int
cmpstringp(常数无效*p1,常数无效*p2)
{
/*此函数的实际参数是“指向
指向char的指针”,但strcmp(3)参数是“指针”
到char”,因此下面的cast加上解引用*/
返回strcmp(*(字符*常量*)p1,*(字符*常量*)p2);
}
int
main(int argc,char*argv[])
{
int j;
断言(argc>1);
qsort(&argv[1],argc-1,sizeof(char*),cmpstringp);
对于(j=1;j

这个问题基本上归结为“如何知道可变长度记录的长度”。需要有某种方法来判断,要么从记录本身,要么从其他数据

一种方法是使用指针/长度对来引用记录——一个指向记录开始的指针和一个长度(int或siZit),它们存储在一个结构中。用C++,你可以使用STD::对,或者用C定义一个LITTEStULT.你可以在这些数组中使用Q排序。


在您的例子中,您可以通过查找字符(10)来判断长度,因为您总是使用它们来终止字符串这是知道的。

看起来比C++更像C……你的比较器功能非常错误——想想你实际上在做什么。为什么你使用了tTMEN,然后直接在其他代码中使用char?
static int
cmpstringp(const void *p1, const void *p2)
{
    /* The actual arguments to this function are "pointers to
      pointers to char", but strcmp(3) arguments are "pointers
       to char", hence the following cast plus dereference */

    return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int
main(int argc, char *argv[])
{
    int j;

    assert(argc > 1);

    qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);

    for (j = 1; j < argc; j++)
        puts(argv[j]);
    exit(EXIT_SUCCESS);
}