结构体数组排序问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结构体数组排序问题相关的知识,希望对你有一定的参考价值。

struct Nation

char country[100];
int gold_medal;
int silver_medal;
int bronze_medal;
int sum;
;
Nation str[10]=
"China",199,119,98,416,
"Korea",76,65,91,232,
"Japan",48,74,94,216,
"Iran",20,14,25,59,
"Kazakhstan",18,23,38,79,
"India",14,17,33,64,
"Chinese_Taipei",13,16,38,67,
"Uzbekistan",11,22,23,56,
"Thailand",11,9,32,52,
"Malaysia",9,18,14,41;
求高手帮我用c++把这个结构体按照country(国家首字母A-Z)和sum(升序)分别排序,谢谢了

参考技术A 使用
void qsort(void *base, size_t nelem, size_t width, int (_USERENTRY *fcmp)(const void *, const void *));
库函数就可以完成了

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

struct Nation

char country[100];
int gold_medal;
int silver_medal;
int bronze_medal;
int sum;
;

Nation str[10]=
"China",199,119,98,416,
"Korea",76,65,91,232,
"Japan",48,74,94,216,
"Iran",20,14,25,59,
"Kazakhstan",18,23,38,79,
"India",14,17,33,64,
"Chinese_Taipei",13,16,38,67,
"Uzbekistan",11,22,23,56,
"Thailand",11,9,32,52,
"Malaysia",9,18,14,41;

int sort_by_country( const void *a, const void *b)

Nation *pa=(Nation *)a,*pb=(Nation *)b;
return( strcmp(pa->country,pb->country) );


int sort_by_sum( const void *a, const void *b)

Nation *pa=(Nation *)a,*pb=(Nation *)b;
if(pa->sum>pb->sum) return 1;
else if(pa->sum==pb->sum) return 0;
else return -1;


int main()

int n;
printf("\nsort by country:\n");
qsort(str,10,sizeof(Nation),sort_by_country);
for(n=0;n<10;n++)

printf("%s %d %d %d %d\n",str[n].country,str[n].gold_medal,
str[n].silver_medal,str[n].bronze_medal,str[n].sum);


printf("\nsort by sum:\n");
qsort(str,10,sizeof(Nation),sort_by_sum);
for(n=0;n<10;n++)

printf("%s %d %d %d %d\n",str[n].country,str[n].gold_medal,
str[n].silver_medal,str[n].bronze_medal,str[n].sum);


getchar();
return 0;
参考技术B #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
struct Nation

char country[100];
int gold_medal;
int silver_medal;
int bronze_medal;
int sum;

;
Nation str[10]=
"China",199,119,98,416,
"Korea",76,65,91,232,
"Japan",48,74,94,216,
"Iran",20,14,25,59,
"Kazakhstan",18,23,38,79,
"India",14,17,33,64,
"Chinese_Taipei",13,16,38,67,
"Uzbekistan",11,22,23,56,
"Thailand",11,9,32,52,
"Malaysia",9,18,14,41;
bool isgreate(const Nation& n1,const Nation& n2)

return n1.sum<n2.sum;

bool operator < (const Nation& n1,const Nation& n2)

return string(n1.country,n1.country+20)<string(n2.country,n2.country+100);

int main()

vector<Nation> vecn;
for(int i=0;i!=10;i++)
vecn.push_back(str[i]);
stable_sort(vecn.begin(),vecn.end());
cout<<"按照country的结果:"<<endl;
for(vector<Nation>::iterator it=vecn.begin();it!=vecn.end();++it)

string ss(it->country,it->country+20);
cout<<ss<<" "<<it->gold_medal<<" "<<it->silver_medal<<" "<<it->bronze_medal<<" "<<it->sum<<endl;

cout<<"按照sum排序的结果:"<<endl;
stable_sort(vecn.begin(),vecn.end(),isgreate);
for(vector<Nation>::iterator it=vecn.begin();it!=vecn.end();++it)

string ss(it->country,it->country+20);
cout<<ss<<" "<<it->gold_medal<<" "<<it->silver_medal<<" "<<it->bronze_medal<<" "<<it->sum<<endl;


return 0;

vs2010编译通过
参考技术C #include "stdio.h"
#include "string.h"

struct Nation

char country[100];
int gold_medal;
int silver_medal;
int bronze_medal;
int sum;
;
Nation str[10]=
"China",199,119,98,416,
"Korea",76,65,91,232,
"Japan",48,74,94,216,
"Iran",20,14,25,59,
"Kazakhstan",18,23,38,79,
"India",14,17,33,64,
"Chinese_Taipei",13,16,38,67,
"Uzbekistan",11,22,23,56,
"Thailand",11,9,32,52,
"Malaysia",9,18,14,41;

void main()

int i,j;
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(strcmp(str[j].country,str[j+1].country)>0)

Nation temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;

for(i=0;i<10;i++)

printf("%s,%d,%d,%d,%d\n",str[i].country,str[i].gold_medal,str[i].silver_medal,str[i].bronze_medal,str[i].sum);

printf("\n");

for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(str[j].sum<str[j+1].sum)

Nation temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;

for(i=0;i<10;i++)

printf("%s,%d,%d,%d,%d\n",str[i].country,str[i].gold_medal,str[i].silver_medal,str[i].bronze_medal,str[i].sum);

printf("\n");
参考技术D #include<iostream>
#include<string.h>
using namespace std;
struct Nation

char country[100];
int gold_medal;
int silver_medal;
int bronze_medal;
int sum;
;
struct Nation str[10]=
"China",199,119,98,416,
"Korea",76,65,91,232,
"Japan",48,74,94,216,
"Iran",20,14,25,59,
"Kazakhstan",18,23,38,79,
"India",14,17,33,64,
"Chinese_Taipei",13,16,38,67,
"Uzbekistan",11,22,23,56,
"Thailand",11,9,32,52,
"Malaysia",9,18,14,41;
void sortbynation()

int i,j;
for(i=1;i<10;i++)

for(j=0;j<10-i;j++)
if(strcmp(str[j].country,str[j+1].country)>0)

struct Nation temp;
temp = str[j];
str[j]= str[j+1];
str[j+1]= temp;


cout<<endl;
for(i=0;i<10;i++)

cout<<str[i].country<<" "<<str[i].gold_medal<<" "<<str[i].silver_medal<<" "<<str[i].bronze_medal<<" "<<str[i].sum<<endl;


void sortbysum()

int i,j;
for(i=1;i<10;i++)

for(j=0;j<10-i;j++)
if(str[j].sum>str[j+1].sum)

struct Nation temp;
temp = str[j];
str[j]= str[j+1];
str[j+1]= temp;


cout<<endl;
for(i=0;i<10;i++)

cout<<str[i].country<<" "<<str[i].gold_medal<<" "<<str[i].silver_medal<<" "<<str[i].bronze_medal<<" "<<str[i].sum<<endl;


int main()

sortbynation();
sortbysum();
return 0;
本回答被提问者采纳

C语言结构体数组排序

我定义了一个动态结构体 并且赋值了 结构体是多类型的 我想要里面的数字排序 请问怎么弄

参考技术A 多定义一个可以比较的数据段(比如整型),然后排序,冒泡排序可以解决问题。注意交换值的时候最好用引用类型&定义一个函数,用函数实现两个结构体的数值互换。

以上是关于结构体数组排序问题的主要内容,如果未能解决你的问题,请参考以下文章

求教一个问题,如何用sort对结构体数组排序

结构体数组定义的排序

如何用sort对结构体进行排序

用vector开的结构体数组,能否用sort排序

c++ sort 函数能对类或者结构体进行排序吗

sort函数能对结构体进行排序吗?