我想要做的是根据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 );
}
答案 0 :(得分:-1)
以下是使用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\n", 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\n");
print_vehicles(vehicles, N_VEHICLES);
printf("\n");
// sort by serial number
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
printf("Sorted by serial number\n");
print_vehicles(vehicles, N_VEHICLES);
printf("\n");
// sort by manufacturer
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
printf("Sorted by manufacturer\n");
print_vehicles(vehicles, N_VEHICLES);
return 0;
}