基于struct内部的元素对指向struct的数组进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于struct内部的元素对指向struct的数组进行排序相关的知识,希望对你有一定的参考价值。

我想在这里做的是根据Struct中的字符串对Structs指针数组进行排序。我认为只需交换数组内的指针而不是交换整个结构就很容易了。所有元素都是动态分配的。我的代码是用德语编写的,所以我只会用英语写出我不懂的部分。提前谢谢,感谢您的帮助。

typedef struct Vehicle {
    char *manufacturer;
    char *serialnumber;
    int weight;
};
void SortByID(struct fahrzeug** data, int Anzahl){ 
    struct Vehicle *temp= (struct Vehicle*)malloc( sizeof(struct Vehicle) );
    int i =0 ;// I think i've written something wrong here ( strcmp)
    while ( i < Anzahl && strcmp(data[i]->serialnumber, data[i+1]->serialnumber) < 0) 
    {
         temp = data[i+1];
         data[i+1] = data[i];
         data[i]=temp ;
         i++;
    }  
    free(temp);
    temp=NULL;
}
int main(){
    int count =0 ;
    struct Vehicle **array = NULL ;
    array=(struct Vehicle**)malloc(  5 * sizeof(struct Vehicle*)  );
    for(int i=0;i<5 ;i++){
       array[i] = (struct Vehicle*)malloc( sizeof(struct Vehicle) ) ;
       count++;
    }
    SortByID (  &array, count ) ; // is this right ?
    return 0; 
}

我也尝试使用qsort函数对其进行排序,但没有成功

int compare(const void *a, const void *b) {
  struct Vehicle * const *one = a;
  struct Vehicle * const *two = b;

  return strcmp((*one)->serialnumber, (*two)->serialnumber);
}
void SortByID(struct Vehicle** data, int Anzahl){
   qsort( data, Anzahl, sizeof(struct Vehicle*) ,compare );
}
答案

这是使用qsort()的一个工作示例。它可以按序列号和制造商进行排序。

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

struct Vehicle {
    char *manufacturer;
    char *serialnumber;
    int weight;
};

int cmp_serialnumber(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->serialnumber, v2->serialnumber);

/*
    // Alternatively You could write this function as this less readable one liner
    return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
                    (*(struct Vehicle * const *)p2)->serialnumber);
*/
}

int cmp_manufacturer(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->manufacturer, v2->manufacturer);
}

void print_vehicles(struct Vehicle **vehicles, int n) {
    for (int i=0; i<n; i++) {
        printf("%s, %s, %d
", vehicles[i]->serialnumber,
                vehicles[i]->manufacturer, vehicles[i]->weight);
    }
}


#define N_VEHICLES  5
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};


int main(){
    struct Vehicle **vehicles = NULL;

    vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));

    for(int i=0; i<N_VEHICLES; i++) {
        vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
        vehicles[i]->manufacturer = manufacturers[i];
        vehicles[i]->serialnumber = serialnumbers[i];
        vehicles[i]->weight = 1000;
    }

    printf("Before
");
    print_vehicles(vehicles, N_VEHICLES);
    printf("
");

    // sort by serial number
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
    printf("Sorted by serial number
");
    print_vehicles(vehicles, N_VEHICLES);
    printf("
");

    // sort by manufacturer
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
    printf("Sorted by manufacturer
");
    print_vehicles(vehicles, N_VEHICLES);

    return 0;
}

以上是关于基于struct内部的元素对指向struct的数组进行排序的主要内容,如果未能解决你的问题,请参考以下文章

struct 内部的 malloc 结构数组

结构struct

C ++:对矢量进行排序 (其中struct有2个整数)基于struct [duplicate]的整数之一

如果将数组用作 struct (C#) 中的元素,它存储在哪里?

C-为啥Struct数组的冒泡排序会跳过Array中的第一个元素?

Linux(内核剖析):14---内核数据结构之链表(struct list_head)