c語言中sprt函數,sprt在程序中怎麼用

本文目錄一覽:

c語言中sort的用法詳解

c語言的學習很多是比較複雜的,那麼c語言中sort的用法的用法你知道嗎?下面我就跟你們詳細介紹下c語言中sort的用法的用法,希望對你們有用。

c語言中sort的用法的用法

sort是STL中提供的算法,頭文件為#includealgorithm以及using namespace std; 函數原型如下:

?

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)進行升序排序,默認操作符為””,第二個版本使用comp函數進行排序控制,comp包含兩個在[first,last)中對應的值,如果使用””則為升序排序,如果使用””則為降序排序,分別對int、float、char以及結構體排序例子如下:

?

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};

//結構比較函數(按照結構中的浮點數值進行排序)

bool compare_struct_float(const product a,const product b){

return a.priceb.price;

}

//結構比較函數(按照結構中的字符串進行排序)

bool compare_struct_str(const product a,const product b){

return string(a.name)string(b.name);

}

//打印函數

void print_int(const int* a,int length){

printf(“升序排序後的int數組:\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數組:\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數組:\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 }};

//整數排序

sort(array_int,array_int+5);

print_int(array_int,5);

//字符排序

sort(array_char,array_char+5);

print_char(array_char,5);

//浮點排序

sort(array_double,array_double+5);

print_double(array_double,5);

//結構中浮點排序

int len = sizeof(structs)/sizeof(struct product);

sort(structs,structs+len,compare_struct_float);

printf(“按結構中float升序排序後的struct數組:\n”);

print_struct_array(structs, len);

//結構中字符串排序

sort(structs,structs+len,compare_struct_str);

printf(“按結構中字符串升序排序後的struct數組:\n”);

print_struct_array(structs, len);

}

sort函數的用法

做ACM題的時候,排序是一種經常要用到的操作。如果每次都自己寫個冒泡之類的O(n^2)排序,不但程序容易超時,而且浪費寶貴的比賽時間,還很有可能寫錯。STL裡面有個sort函數,可以直接對數組排序,複雜度為n*log2(n)。使用這個函數,需要包含頭文件。

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

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

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

排序的數據類型不局限於整數,只要是定義了小於運算的類型都可以,比如字符串類string。

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

?

1

2

3

4

bool cmp(int a,int b)

{

return ab;

}

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

假設自己定義了一個結構體node

?

1

2

3

4

5

struct node{

int a;

int b;

double c;

}

有一個node類型的數組node arr[100],想對它進行排序:先按a值升序排列,如果a值相同,再按b值降序排列,如果b還相同,就按c降序排列。就可以寫這樣一個比較函數:

以下是代碼片段:

?

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函數的用法:對int類型數組排序

?

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函數的用法:對char類型數組排序(同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函數的用法:對double類型數組排序(特別要注意)

?

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函數的用法:對結構體一級排序

?

1

2

3

4

5

6

7

8

9

10

11

struct In

{

double data;

int other;

}s[100]

//按照data的值從小到大將結構體排序,關於結構體內的排序關鍵數據data的類型可以很多種,參考上面的例子寫

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

{

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

}

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

sort函數的用法:對結構體

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

struct In

{

int x;

int y;

}s[100];

//按照x從小到大排序,當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函數的用法:對字符串進行排序

?

1

2

3

4

5

6

7

8

9

10

11

struct In

{

int data;

char str[100];

}s[100];

//按照結構體中字符串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函數的用法:計算幾何中求凸包的cmp

?

1

2

3

4

5

6

7

8

9

int cmp(const void *a,const void *b) //重點cmp函數,把除了1點外的所有點,旋轉角度排序

{

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)) //如果在一條直線上,則把遠的放在前面

return 1;

else return -1;

}

猜你喜歡:

1. c中的用法

2. c語言中邏輯或的用法

3. c語言strcmp的用法

4. c語言中free的用法

5. c語言pow的用法

6. c語言中putchar的用法

sort函數在C語言中的作用是啥?

1、sort()函數描述:對給定區間所有元素進行排序。

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

2、sort()函數舉例:

#include algorithm

#include iostream

using namespace std;

main()

{

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

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

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

}

C語言. .編寫一個Sort函數,完成對整型數組元素升序排列。

#include stdio.h

void sort(int a[], int n) {//選擇排序

int i,j,k,t;

for(i = 0; i  n – 1; ++i) {

k = i;

for(j = k + 1; j  n; ++j) {

if(a[k]  a[j]) k = j;

}

if(k != i) {

t = a[i];

a[i] = a[k];

a[k] = t;

}

}

}

int main() {

int a[] = {21,16,30,21,8,19,33,26,28,27,24,50,13,12};

int i,n = sizeof(a)/sizeof(a[0]);

printf(“排序前:\n”);

for(i = 0; i  n; ++i)

printf(“%d “,a[i]);

printf(“\n”);

sort(a,n);

printf(“排序後:\n”);

for(i = 0; i  n; ++i)

printf(“%d “,a[i]);

printf(“\n”);

return 0;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246800.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 13:17
下一篇 2024-12-12 13:18

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字符串操作中,capitalize函數常常被用到,這個函數可以使字符串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • 單片機打印函數

    單片機打印是指通過串口或並口將一些數據打印到終端設備上。在單片機應用中,打印非常重要。正確的打印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的打印數據可以幫助我們快速…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變量時顯示的指定變量類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29

發表回復

登錄後才能評論