簡體   English   中英

不使用數組對結構進行排序

[英]Not sorting of struct using arrays

這似乎只是一個簡單的練習,但是以下qsort算法無法正常工作。 struct abcSort正確顯示所有分配的值。 不幸的是, qsort沒有排序任何東西。

typedef int(*compfn)(const void*, const void*);

struct ABCSort
{
    int svSort[10];
    string itemSort[10];
};

struct ABCSort abcSort;

int compare(struct ABCSort*, struct ABCSort*);

void mainSort()
{   
    for (int i = 0; i < 10; i++)
    {
        abcSort.svSort[i] = 100 - i;
        abcSort.itemSort[i] = arrayREAD[i][2];
    }

    qsort( (void*)&abcSort, 10, sizeof(struct ABCSort), (compfn)compare );
}

int compare(struct ABCSort *elem1, struct ABCSort *elem2)
{
    if (elem1->svSort< elem2->svSort)
        return -1;
    else if (elem1->svSort > elem2->svSort)
        return 1;
    else
        return 0;
}

您似乎打算對整數數組進行排序(您的compare函數看起來像這樣)。 但是,實際上要交給qsort進行排序的是指向一個結構的指針,該結構包含數組。

因此,您實際上要嘗試排序的是一個已初始化的單一結構ABCSort和其他9個未初始化的結構。 這必須失敗。

您的qsort行應如下所示:

qsort ((void*)&(abcsort.svsort), 10, sizeof (int), (compfn)compare);

另外,您應該更改比較函數,以使其接受並作用於兩個指向整數的指針:

int compare (int * e1, int * e2) {
   return *e1 - *e2;
}

編輯:更好地解釋了您想要什么之后,請看以下內容:

typedef int(compfn)(const void, const void*); 

#define MAXCARS 5

struct car { 
   int sortKey;
   double displacement; 
   char name[15];  /* Note I have decided this should be C */
}; 

/* This time we have an array of structs */
struct car cars [MAXCARS] = { 
   { 0, 1.9, "BMW" }, 
   { 0, 6.3, "Audi" }, 
   { 0, 0.5, "Fiat" },
   { 0, 25.0, "Humvee" },
   { 0, 0.05, "Minibike" }
};

int compare(struct car*, struct car*); 

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

   for (i = 0; i < MAXCARS; i++)
       cars[i].sortKey = 100 - i;

   qsort((void *)&cars, MAXCARS, sizeof(struct car), (compfn)compare); 
} 

/* Note we are comparing struct car-s in here, based on their displacement */
int compare(struct car *elem1, struct car *elem2) { 
   return elem1->sortKey - elem2->sortKey;
} 

您已經構建了兩個由整數和字符串組成的數組,並且希望按數字對它們進行排序,並保持初始配對。 那是第一個問題,您應該創建一個結構數組,每個結構包含一個數字和一個字符串,以及一個比較該結構的整數成員以獲得排序順序的函數。

您也將此問題標記為C ++,但是您正在使用qsort ,數組和函數指針(如C),因此我將介紹兩個完整的C程序來解決您的問題。


讓我們看一下,使用結構數組 ,您的代碼可能是什么樣的:

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

#define MAX 6
#define CMAX 15

typedef int(*cmp_fn)(const void*, const void*);

typedef struct {
    int num;
    char str[CMAX];
} numABC;

int comp_struct(const numABC *lhs, const numABC *rhs ) { 
    if ( lhs->num < rhs->num ) 
        return -1; 
    if ( lhs->num > rhs->num ) 
        return 1; 
    return 0;
} 

int main(void) {

    numABC myArray[MAX] = { {6, "cat"}, {4, "dog"}, {8, "panter"},
                         {2, "red fish"}, {1, "hawk"}, {6, "snake"} };
    int i;

    // sort the array of structs by int member
    qsort(myArray, MAX, sizeof(numABC), (cmp_fn)comp_struct);

    // print out the sorted array
    printf("\nSort by numbers:\n");
    for ( i = 0; i < MAX; ++i ) {
        printf("%d %s\n",myArray[i].num,myArray[i].str);
    }

    return 0;
}

如果要使用此代碼,但是要有兩個數組,則一種方法是轉換這些數組:

int nums[MAX] = {6,4,8,2,1,3};
char str[MAX][CMAX] = {"cat","dog","panter","red fish","hawk","snake"};
int i;

// create the array of structs from the two arrays
numABC myArray[MAX];
for ( i = 0; i < MAX; ++i ) {
    myArray[i].num = nums[i];
    strcpy(myArray[i].str, str[i]);
}



兩個不同的數組進行排序的另一種方法(保持兩個數組之間的配對或對齊)是使用更復雜的方法,該方法包括對索引數組進行排序。 為了保持兩個原始數組之間的關系,我將必須使用可在比較函數內部訪問的全局變量。 索引排序后,原始數組將相應更改。

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

#define MAX 6
#define CMAX 15

const size_t I_DIM = MAX * sizeof(int);
const size_t SS_DIM = MAX * sizeof(char*);
const size_t S_DIM = MAX * CMAX;

// global variables needed to perform comparisons   
int *pg_int;
char **pg_str;

typedef int(*cmp_fn)(const void*, const void*);

int comp_num(const int *lhs, const int *rhs ) { 
    if (pg_int[*lhs] < pg_int[*rhs]) 
        return -1; 
    if (pg_int[*lhs] > pg_int[*rhs]) 
        return 1; 
    return 0;
} 

int comp_str(const int *lhs, const int *rhs ) { 
    return strcmp(pg_str[*lhs],pg_str[*rhs]);
} 

int main(void) {
    int nums[MAX] = {6,4,8,2,1,3};
    char str[MAX][CMAX] = {"cat","dog","panter","red fish","hawk","snake"};
    int i;

    // create an array of indeces 
    int index[MAX];
    for ( i = 0; i < MAX; ++i ) {
        index[i] = i;
    }

    // set global copies
    pg_int = malloc(I_DIM);
    memcpy(pg_int,nums,I_DIM);
    pg_str = malloc(SS_DIM);
    pg_str[0] = malloc(S_DIM);
    memcpy(pg_str[0],str[0],S_DIM);
    for ( i = 1; i < MAX; i++ ) {
        pg_str[i] = pg_str[0] + i * CMAX;
    }

    // sort the indeces ordering by ints
    qsort(index, MAX, sizeof(int), (cmp_fn)comp_num);

    //update the two arrays
    for ( i = 0; i < MAX; ++i ) {
        nums[i] = pg_int[index[i]];
        strcpy(str[i],pg_str[index[i]]);
    }

    // print out sorted couples
    printf("Sort by numbers:\n");
    for ( i = 0; i < MAX; ++i ) {
        printf("%d %s\n",nums[i],str[i]);
    }

    // sort the indeces ordering by strings
    qsort(index, MAX, sizeof(int), (cmp_fn)comp_str);

    //update the two arrays
    for ( i = 0; i < MAX; ++i ) {
        nums[i] = pg_int[index[i]];
        strcpy(str[i],pg_str[index[i]]);
    }

    // print out sorted couples
    printf("\nSort by strings:\n");
    for ( i = 0; i < MAX; ++i ) {
        printf("%d %s\n",nums[i],str[i]);
    }

    free(pg_int);
    free(pg_str[0]);
    free(pg_str);
    return 0;
}

輸出(對不起的示例很抱歉)是:

Sort by numbers:
1 hawk
2 red fish
3 snake
4 dog
6 cat
8 panter

Sort by strings:
6 cat
4 dog
1 hawk
8 panter
2 red fish
3 snake




如果要在C ++中完成相同的任務,則應利用標准庫並使用std::vector作為容器,並使用std::sort作為算法:

#include <iostream>
#include <vector>
#include <algorithm>

using std::vector;
using std::string;
using std::cout;

struct numABC {
    double num;
    string str;

    // instead of a compare function I can overload operator <
    friend bool operator<( const numABC &lhs, const numABC &rhs ) {
        return lhs.num < rhs.num;
    }
};

// or use a lambda
auto cmp_str = []( const numABC &lhs, const numABC &rhs ) -> bool {
    return lhs.str < rhs.str;
};

int main() {

    vector<numABC> my_data = { {3.6, "cat"}, {5.7, "dog"}, {7.1, "panter"},
                         {0.2, "red fish"}, {1.8, "hawk"}, {1.1, "snake"}};

    std::sort(my_data.begin(), my_data.end());

    std::cout << "Sort by numbers:\n";
    for ( auto & s : my_data ) {
        std::cout << s.num << ' ' << s.str << '\n';
    }

    std::sort(my_data.begin(), my_data.end(), cmp_str);
    // passing a lambda to specify how to compare ^^

    std::cout << "Sort by strings:\n";
    // if you don't like c++11 range for:
    for ( int i = 0; i < my_data.size(); ++i ) {
        std::cout << my_data[i].num << ' ' << my_data[i].str << '\n';
    }

    return 0;
}


請注意,我已將my_data初始化為my_data類型的對象的向量。 如果必須從兩個數組開始,則可以這樣創建向量:

vector<double> nums = {3.6, 5.7, 7.1, 0.2, 1.8, 1.1};
vector<string> str = {"cat", "dog", "panter", "red fish", "hawk", "snake"};
vector<numABC> my_data;

for ( int i = 0; i < nums.size(); ++i ) {
    my_data.push_back(numABC{nums[i],str[i]});
}

排序后,如果必須再次提取兩個向量(而不是簡單地遍歷my_data),則可以執行以下操作:

for ( int i = 0; i < my_data.size(); ++i ) {
    nums[i] = my_data[i].num;
    str[i] = my_data[i].str;
}




或者,您可以實現與我之前使用的算法相似的算法,並使用 nums 的輔助向量對 兩個向量 numssrt 進行 排序

vector<double> nums = {3.6, 5.7, 7.1, 0.2, 1.8, 1.1};
vector<string> str = {"cat", "dog", "panter", "red fish", "hawk", "snake"};

// create the vector of indeces
vector<int> idx(nums.size());
std::iota(idx.begin(),idx.end(),0); // fill the vector, require #include <numeric>

// thanks to the lambda variable capture you don't need globals
auto cmp_ind = [ &nums ]
           ( const int &lhs, const int &rhs ) -> bool {
    return nums[lhs] < nums[rhs];
};

// sort indeces 
std::sort(idx.begin(), idx.end(),cmp_ind);

// create sorted arrays. It could be done in place but it's more difficult
vector<double> sorted_nums(nums.size());
vector<string> sorted_str(str.size());
for ( int i = 0; i < nums.size(); ++i ) {
    sorted_nums[i] = nums[idx[i]];
    sorted_str[i] = str[idx[i]];
}

std::cout << "Sort by numbers:\n";
for ( int i = 0; i < nums.size(); ++i ) {
    std::cout << sorted_nums[i] << ' ' << sorted_str[i] << '\n';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM