十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
自定義一個(gè)比較函數(shù),直接調(diào)用快排庫(kù)函數(shù)qsort即可。舉例如下:
公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出興隆臺(tái)免費(fèi)做網(wǎng)站回饋大家。
//#include?"stdafx.h"http://If?the?vc++6.0,?with?this?line.
#include?"stdio.h"
#include?"stdlib.h"
#include?"time.h"
struct?ln{
int?data,score,math;
}ss[100];
int?mycmp(const?void?*a,?const?void?*b){//自定義比較函數(shù)
return?((struct?ln*)a)-data-((struct?ln*)b)-data;//若要降序,-號(hào)前后變量交換
}//data改為score或math就可按相應(yīng)要素排序
int?main(void){//測(cè)試一下……
int?i;
srand((unsigned)time(NULL));
for(i=0;i100;ss[i++].data=rand()%1000);
qsort(ss,100,sizeof(struct?ln),mycmp);
for(i=0;i100;printf("%4d",ss[i++].data));
printf("\n");
return?0;
}
#includestdio.h
struct
{
long long id;
char name[32];
int s;
}t,stus[200];
int main()
{
int n,i,swap=1,sorted=0;
scanf("%d",n);
for(i=0;in;i++)
scanf("%lld %s %d",stus[i].id,stus[i].name,stus[i].s);
printf("\n");
while(swap)
{
swap=0;
for(i=0;in-1-sorted;i++)
{
if(stus[i+1].sstus[i].s)
{
swap=1;
t=stus[i];
stus[i]=stus[i+1];
stus[i+1]=t;
}
}
sorted++;
}
for(i=0;in;i++)
printf("%lld %s %d\n",stus[i].id,stus[i].name,stus[i].s);
return 0;
}
c語(yǔ)言的學(xué)習(xí)很多是比較復(fù)雜的,那么c語(yǔ)言中sort的用法的用法你知道嗎?下面我就跟你們?cè)敿?xì)介紹下c語(yǔ)言中sort的用法的用法,希望對(duì)你們有用。
c語(yǔ)言中sort的用法的用法
sort是STL中提供的算法,頭文件為#includealgorithm以及using namespace std; 函數(shù)原型如下:
?
1
2
3
4
5
template class RandomAccessIterator
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template class RandomAccessIterator, class Compare
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
使用第一個(gè)版本是對(duì)[first,last)進(jìn)行升序排序,默認(rèn)操作符為"",第二個(gè)版本使用comp函數(shù)進(jìn)行排序控制,comp包含兩個(gè)在[first,last)中對(duì)應(yīng)的值,如果使用""則為升序排序,如果使用""則為降序排序,分別對(duì)int、float、char以及結(jié)構(gòu)體排序例子如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#includestdio.h
#includealgorithm
#includestring
using namespace std;
struct product{
char name[16];
float price;
};
int array_int[5]={4,1,2,5,3};
char array_char[5]={'a','c','b','e','d'};
double array_double[5]={1.2,2.3,5.2,4.6,3.5};
//結(jié)構(gòu)比較函數(shù)(按照結(jié)構(gòu)中的浮點(diǎn)數(shù)值進(jìn)行排序)
bool compare_struct_float(const product a,const product b){
return a.priceb.price;
}
//結(jié)構(gòu)比較函數(shù)(按照結(jié)構(gòu)中的字符串進(jìn)行排序)
bool compare_struct_str(const product a,const product b){
return string(a.name)string(b.name);
}
//打印函數(shù)
void print_int(const int* a,int length){
printf("升序排序后的int數(shù)組:\n");
for(int i=0; ilength-1; i++)
printf("%d ",a[i]);
printf("%d\n",a[length-1]);
}
void print_char(const char* a,int length){
printf("升序排序后的char數(shù)組:\n");
for(int i=0; ilength-1; i++)
printf("%c ",a[i]);
printf("%c\n",a[length-1]);
}
void print_double(const double* a,int length){
printf("升序排序后的dobule數(shù)組:\n");
for(int i=0; ilength-1; i++)
printf("%.2f ",a[i]);
printf("%.2f\n",a[length-1]);
}
void print_struct_array(struct product *array, int length)
{
for(int i=0; ilength; i++)
printf("[ name: %s \t price: $%.2f ]\n", array[i].name, array[i].price);
puts("--");
}
void main()
{
struct product structs[] = {{"mp3 player", 299.0f}, {"plasma tv", 2200.0f},
{"notebook", 1300.0f}, {"smartphone", 499.99f},
{"dvd player", 150.0f}, {"matches", 0.2f }};
//整數(shù)排序
sort(array_int,array_int+5);
print_int(array_int,5);
//字符排序
sort(array_char,array_char+5);
print_char(array_char,5);
//浮點(diǎn)排序
sort(array_double,array_double+5);
print_double(array_double,5);
//結(jié)構(gòu)中浮點(diǎn)排序
int len = sizeof(structs)/sizeof(struct product);
sort(structs,structs+len,compare_struct_float);
printf("按結(jié)構(gòu)中float升序排序后的struct數(shù)組:\n");
print_struct_array(structs, len);
//結(jié)構(gòu)中字符串排序
sort(structs,structs+len,compare_struct_str);
printf("按結(jié)構(gòu)中字符串升序排序后的struct數(shù)組:\n");
print_struct_array(structs, len);
}
sort函數(shù)的用法
做ACM題的時(shí)候,排序是一種經(jīng)常要用到的操作。如果每次都自己寫(xiě)個(gè)冒泡之類(lèi)的O(n^2)排序,不但程序容易超時(shí),而且浪費(fèi)寶貴的比賽時(shí)間,還很有可能寫(xiě)錯(cuò)。STL里面有個(gè)sort函數(shù),可以直接對(duì)數(shù)組排序,復(fù)雜度為n*log2(n)。使用這個(gè)函數(shù),需要包含頭文件。
這個(gè)函數(shù)可以傳兩個(gè)參數(shù)或三個(gè)參數(shù)。第一個(gè)參數(shù)是要排序的區(qū)間首地址,第二個(gè)參數(shù)是區(qū)間尾地址的下一地址。也就是說(shuō),排序的區(qū)間是[a,b)。簡(jiǎn)單來(lái)說(shuō),有一個(gè)數(shù)組int a[100],要對(duì)從a[0]到a[99]的元素進(jìn)行排序,只要寫(xiě)sort(a,a+100)就行了,默認(rèn)的排序方式是升序。
拿我出的“AC的策略”這題來(lái)說(shuō),需要對(duì)數(shù)組t的第0到len-1的元素排序,就寫(xiě)sort(t,t+len);
對(duì)向量v排序也差不多,sort(v.begin(),v.end());
排序的數(shù)據(jù)類(lèi)型不局限于整數(shù),只要是定義了小于運(yùn)算的類(lèi)型都可以,比如字符串類(lèi)string。
如果是沒(méi)有定義小于運(yùn)算的數(shù)據(jù)類(lèi)型,或者想改變排序的順序,就要用到第三參數(shù)——比較函數(shù)。比較函數(shù)是一個(gè)自己定義的函數(shù),返回值是bool型,它規(guī)定了什么樣的關(guān)系才是“小于”。想把剛才的整數(shù)數(shù)組按降序排列,可以先定義一個(gè)比較函數(shù)cmp
?
1
2
3
4
bool cmp(int a,int b)
{
return ab;
}
排序的時(shí)候就寫(xiě)sort(a,a+100,cmp);
假設(shè)自己定義了一個(gè)結(jié)構(gòu)體node
?
1
2
3
4
5
struct node{
int a;
int b;
double c;
}
有一個(gè)node類(lèi)型的數(shù)組node arr[100],想對(duì)它進(jìn)行排序:先按a值升序排列,如果a值相同,再按b值降序排列,如果b還相同,就按c降序排列。就可以寫(xiě)這樣一個(gè)比較函數(shù):
以下是代碼片段:
?
1
2
3
4
5
6
bool cmp(node x,node y)
{
if(x.a!=y.a) return x.a
if(x.b!=y.b) return x.by.b;
return return x.cy.c;
}
排序時(shí)寫(xiě)sort(arr,a+100,cmp);
?
1
2
3
4
5
qsort(s[0],n,sizeof(s[0]),cmp);
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
sort函數(shù)的用法:對(duì)int類(lèi)型數(shù)組排序
?
1
2
3
4
5
6
7
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
sort函數(shù)的用法:對(duì)char類(lèi)型數(shù)組排序(同int類(lèi)型)
?
1
2
3
4
5
6
7
char word[100];
Sample:
int cmp( const void *a , const void *b )
{
return *(char *)a - *(int *)b;
}
qsort(word,100,sizeof(word[0]),cmp);
sort函數(shù)的用法:對(duì)double類(lèi)型數(shù)組排序(特別要注意)
?
1
2
3
4
5
6
double in[100];
int cmp( const void *a , const void *b )
{
return *(double *)a *(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
sort函數(shù)的用法:對(duì)結(jié)構(gòu)體一級(jí)排序
?
1
2
3
4
5
6
7
8
9
10
11
struct In
{
double data;
int other;
}s[100]
//按照data的值從小到大將結(jié)構(gòu)體排序,關(guān)于結(jié)構(gòu)體內(nèi)的排序關(guān)鍵數(shù)據(jù)data的類(lèi)型可以很多種,參考上面的例子寫(xiě)
int cmp( const void *a ,const void *b)
{
return ((In *)a)-data - ((In *)b)-data ;
}
qsort(s,100,sizeof(s[0]),cmp);
sort函數(shù)的用法:對(duì)結(jié)構(gòu)體
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct In
{
int x;
int y;
}s[100];
//按照x從小到大排序,當(dāng)x相等時(shí)按照y從大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c-x != d-x) return c-x - d-x;
else return d-y - c-y;
}
qsort(s,100,sizeof(s[0]),cmp);
sort函數(shù)的用法:對(duì)字符串進(jìn)行排序
?
1
2
3
4
5
6
7
8
9
10
11
struct In
{
int data;
char str[100];
}s[100];
//按照結(jié)構(gòu)體中字符串str的字典順序排序
int cmp ( const void *a , const void *b )
{
return strcmp( ((In *)a)-str , ((In *)b)-str );
}
qsort(s,100,sizeof(s[0]),cmp);
sort函數(shù)的用法:計(jì)算幾何中求凸包的cmp
?
1
2
3
4
5
6
7
8
9
int cmp(const void *a,const void *b) //重點(diǎn)cmp函數(shù),把除了1點(diǎn)外的所有點(diǎn),旋轉(zhuǎn)角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) 0) return 1;
else if( !calc(*c,*d,p[1]) dis(c-x,c-y,p[1].x,p[1].y) dis(d-x,d-y,p[1].x,p[1].y)) //如果在一條直線上,則把遠(yuǎn)的放在前面
return 1;
else return -1;
}
猜你喜歡:
1. c中的用法
2. c語(yǔ)言中邏輯或的用法
3. c語(yǔ)言strcmp的用法
4. c語(yǔ)言中free的用法
5. c語(yǔ)言pow的用法
6. c語(yǔ)言中putchar的用法
設(shè)結(jié)構(gòu)體名為AAA,結(jié)構(gòu)體數(shù)組聲明為struct AAA a[N];(N為宏定義常量),身份證成員名為id,則排序函數(shù)可如下寫(xiě)——
#include?"stdio.h"
#include?string.h
#define?N?3
struct?AAA{
char?id[20];
int?age;
};
void?mysort(struct?AAA?*p){//排序函數(shù)
struct?AAA?t;
int?i,j,k;
for(i=0;iN;i++){
for(k=i,j=k+1;jN;j++)
if(strcmp((p+j)-id,(p+k)-id)0)
k=j;
if(i!=k)
t=*(p+k),*(p+k)=*(p+i),*(p+i)=t;
}
}
int?main(int?argc,char?*argv[]){//測(cè)試主函數(shù)
struct?AAA?a[N]={{"650104194812109907",77},{"333018201801015555",1},{"650104194812109903",80}};
mysort(a);
printf("%s\t%d\n",a[0].id,a[0].age);
printf("%s\t%d\n",a[1].id,a[1].age);
printf("%s\t%d\n",a[2].id,a[2].age);
return?0;
}
運(yùn)行結(jié)果: