欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

sort在c語言的庫函數(shù),c語言有sort函數(shù)嗎

C語言sort函數(shù)如何使用

#includestdio.h??

成都創(chuàng)新互聯(lián)公司長期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為阜城企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作阜城網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

#includestdlib.h??

int?comp(const?void*a,const?void*b)//用來做比較的函數(shù)。??

{??

return?*(int*)a-*(int*)b;??

}??

int?main()??

{??

int?a[10]?=?{2,4,1,5,5,3,7,4,1,5};//亂序的數(shù)組。??

int?i;??

qsort(a,10,sizeof(int),comp);//調(diào)用qsort排序??

for(i=0;i10;i++)//輸出排序后的數(shù)組??

{??

printf("%d?",a[i]);??

}??

return?0;??

}

c語言和c++中,對于sort函數(shù)的使用,不同。c語言中沒有預(yù)置的sort函數(shù),如果在c語言中,要調(diào)用sort函數(shù),就需要自定義一個用于排序的函數(shù),或者使用c語言自有的qsort函數(shù),其頭文件為stdlib.h。

c 排序—sort()函數(shù)

代碼片段:

struct?STU{

char?name[13];

int?chinese,?math,?english;

int?total;

}stu[20];

bool?cmp(STU?a,?STU?b)?

{

return?a.total??b.total;

}

void?sort()

{

std::sort(stu,?stu?+?n,?cmp);//n是總共的學(xué)生個數(shù)

printf("name\t\tchinese\tmath\tenglish\ttotal\n");

for?(int?i?=?0;?i??n;?i++)?{

printf("%12s",?stu[i].name);

printf("%8d",?stu[i].chinese);

printf("%8d",?stu[i].math);

printf("%8d",?stu[i].english);

printf("%8d",?stu[i].total);

printf("\n");

}

}

void?menu()

{

int?a;

cin??a;

switch(a)?{

case?6?:?sort();

}

}

c語言中sort的用法詳解

c語言的學(xué)習(xí)很多是比較復(fù)雜的,那么c語言中sort的用法的用法你知道嗎?下面我就跟你們詳細(xì)介紹下c語言中sort的用法的用法,希望對你們有用。

c語言中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 );

使用第一個版本是對[first,last)進(jìn)行升序排序,默認(rèn)操作符為"",第二個版本使用comp函數(shù)進(jìn)行排序控制,comp包含兩個在[first,last)中對應(yīng)的值,如果使用""則為升序排序,如果使用""則為降序排序,分別對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題的時候,排序是一種經(jīng)常要用到的操作。如果每次都自己寫個冒泡之類的O(n^2)排序,不但程序容易超時,而且浪費(fèi)寶貴的比賽時間,還很有可能寫錯。STL里面有個sort函數(shù),可以直接對數(shù)組排序,復(fù)雜度為n*log2(n)。使用這個函數(shù),需要包含頭文件。

這個函數(shù)可以傳兩個參數(shù)或三個參數(shù)。第一個參數(shù)是要排序的區(qū)間首地址,第二個參數(shù)是區(qū)間尾地址的下一地址。也就是說,排序的區(qū)間是[a,b)。簡單來說,有一個數(shù)組int a[100],要對從a[0]到a[99]的元素進(jìn)行排序,只要寫sort(a,a+100)就行了,默認(rèn)的排序方式是升序。

拿我出的“AC的策略”這題來說,需要對數(shù)組t的第0到len-1的元素排序,就寫sort(t,t+len);

對向量v排序也差不多,sort(v.begin(),v.end());

排序的數(shù)據(jù)類型不局限于整數(shù),只要是定義了小于運(yùn)算的類型都可以,比如字符串類string。

如果是沒有定義小于運(yùn)算的數(shù)據(jù)類型,或者想改變排序的順序,就要用到第三參數(shù)——比較函數(shù)。比較函數(shù)是一個自己定義的函數(shù),返回值是bool型,它規(guī)定了什么樣的關(guān)系才是“小于”。想把剛才的整數(shù)數(shù)組按降序排列,可以先定義一個比較函數(shù)cmp

?

1

2

3

4

bool cmp(int a,int b)

{

return ab;

}

排序的時候就寫sort(a,a+100,cmp);

假設(shè)自己定義了一個結(jié)構(gòu)體node

?

1

2

3

4

5

struct node{

int a;

int b;

double c;

}

有一個node類型的數(shù)組node arr[100],想對它進(jìn)行排序:先按a值升序排列,如果a值相同,再按b值降序排列,如果b還相同,就按c降序排列。就可以寫這樣一個比較函數(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;

}

排序時寫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ù)的用法:對int類型數(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ù)的用法:對char類型數(shù)組排序(同int類型)

?

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ù)的用法:對double類型數(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ù)的用法:對結(jié)構(gòu)體一級排序

?

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的類型可以很多種,參考上面的例子寫

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

{

return ((In *)a)-data - ((In *)b)-data ;

}

qsort(s,100,sizeof(s[0]),cmp);

sort函數(shù)的用法:對結(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相等時按照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ù)的用法:對字符串進(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ù)的用法:計算幾何中求凸包的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語言中邏輯或的用法

3. c語言strcmp的用法

4. c語言中free的用法

5. c語言pow的用法

6. c語言中putchar的用法

sort函數(shù)在C語言中的作用是啥?

1、sort()函數(shù)描述:對給定區(qū)間所有元素進(jìn)行排序。

sort()函數(shù)語法:sort(begin,end),表示一個范圍。

2、sort()函數(shù)舉例:

#include?algorithm

#include?iostream

using?namespace?std;

main()

{

int?a[11]={2,4,8,5,7,1,10,6,9,3};//a的長度=待排數(shù)據(jù)個數(shù)+1

sort(a,a+10);//對[a,a+10)排序

for(int?i=0;i10;++i)?couta[i]endl;

}

C語言的“冒泡排序”怎么做?sort(str)什么意思?

對于冒泡:

可以這樣考慮

外層循環(huán)式控制一共有多少個泡需要排序, 這個當(dāng)然要用循環(huán)

內(nèi)層循環(huán)控制把某一個泡放到正確的位置, 這個也要用循環(huán), 因為這個泡要和所有未排序泡比較一遍, 然后才能知道自己應(yīng)該處的位置

這里有兩個點(diǎn),明白了的話,這道題就明白了

1. 外層循環(huán): 僅僅控制一共有多少個泡需要排序, 比如代碼中a[10], 一共是10個元素

2. 內(nèi)層循環(huán): 僅僅控制把當(dāng)前最大的泡放到最后, 也就是一次內(nèi)層循環(huán),僅僅把最大的那個泡放到最后了而已

把1和2綜合起來看

當(dāng)j=0時,把a(bǔ)數(shù)組10個元素中最大的泡放到最后

當(dāng)j=1時,把a(bǔ)數(shù)組10個元素中第二大的元素放到倒數(shù)第二個位置

依次類推..直到第10大的元素,即最小的元素放到正數(shù)第一個位置

樓主可以這樣測試一下,會更加清晰,把第一個循環(huán)去掉, 內(nèi)層循環(huán)改為:

for(i=0; i 10; i++) 相當(dāng)于僅執(zhí)行上述j=0的一次內(nèi)層循環(huán), 這個循環(huán)的作用是把最大的元素放到最后

在解釋下內(nèi)層循環(huán)為什么是 i10-j 其實這個不難理解

當(dāng)j=0時, 相當(dāng)于 for(i=0;i10;i++) 也就是遍歷所有元素,把最大的放到最后

當(dāng)j=1時, 相當(dāng)于 for(i=0;i9;i++) 這次僅僅遍歷前9個元素,把前9個元素中最大的放到第九個位置, 因為第10個位置已經(jīng)是最大的了,不用再次比較了.

如果實在是對嵌套循環(huán)理解不了,還有個簡單的辦法,把外層循環(huán)解開:

也就是可以這么寫:

//----------------------------------------

for(i=0;i10;i++) //等價于j=0

if (a[i]a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

for(i=0;i9;i++) //等價于j=1

if (a[i]a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

...//同上,僅僅是把i后面的值遞減,直到

for(i=0;i1;i++) //等價于j=9

if (a[i]a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

上面這一系列代碼,等價于原來的雙層嵌套循環(huán)

sort(str) 就是用戶自定義的冒泡排序函數(shù)的函數(shù)名,str作為函數(shù)參數(shù)

c語言的排序函數(shù)在哪個庫文件中

在stdlib.h頭文件中。

有qsort() //快速排序

qsort函數(shù),也就是快速排序算法,在C的stdlib庫中,需加入頭文件#include cstdlib 或#include stdlib.h。

調(diào)用qsort函數(shù)需要寫cmp比較函數(shù)。

給出按升序排列的例子:

int cmp(const void* a, const void* b)//注意這里是int{return (int*)a - (int*)b;}

調(diào)用:

qsort(a, n, sizeof(int), cmp);//a為數(shù)組,n為個數(shù)

如果需要按照自己的意愿排列,那么同樣重寫cmp比較函數(shù),就可以完成,和sort函數(shù)類似。時間復(fù)雜度為O(n log n),但是某些情況要比sort函數(shù)好。

網(wǎng)頁標(biāo)題:sort在c語言的庫函數(shù),c語言有sort函數(shù)嗎
URL鏈接:http://chinadenli.net/article33/dsgepss.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站設(shè)計公司面包屑導(dǎo)航關(guān)鍵詞優(yōu)化Google網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)