重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
c語言的學習很多是比較復雜的,那么c語言中sort的用法的用法你知道嗎?下面我就跟你們詳細介紹下c語言中sort的用法的用法,希望對你們有用。
綏棱ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創新互聯公司的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
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的用法
#includestdio.h??
#includestdlib.h??
int?comp(const?void*a,const?void*b)//用來做比較的函數。??
{??
return?*(int*)a-*(int*)b;??
}??
int?main()??
{??
int?a[10]?=?{2,4,1,5,5,3,7,4,1,5};//亂序的數組。??
int?i;??
qsort(a,10,sizeof(int),comp);//調用qsort排序??
for(i=0;i10;i++)//輸出排序后的數組??
{??
printf("%d?",a[i]);??
}??
return?0;??
}
c語言和c++中,對于sort函數的使用,不同。c語言中沒有預置的sort函數,如果在c語言中,要調用sort函數,就需要自定義一個用于排序的函數,或者使用c語言自有的qsort函數,其頭文件為stdlib.h。
#include stdio.h
#include stdlib.h
#include string.h
FILE *f1,*f2;
char file1[20],file2[20],name[200][20],name1[15];
int n,m,data[200][10];
void init(){
int i,j;
char ch=' ';
printf("%20c歡迎使用班級成績管理系統\n\n",ch);
printf("請輸入輸入和輸出文件名(中間用一個空格隔開):\n");
scanf("%s %s",file1,file2);// 1)該條語句的功能是什么?
f1=fopen(file1,"r");
f2=fopen(file2,"w");
fscanf(f1,"%d%d",n,m); // 2)該條語句的功能是什么?
for(i=1;i=n;i++)
{
fscanf(f1,"%s",name[i]); // 3)該條語句的功能是什么?
data[i][0]=0; // 4)該條語句的功能是什么?沒有這條語句可不可以?
for(j=1;j=m;j++)
{
fscanf(f1,"%d",data[i][j]);
data[i][0]+=data[i][j]; // 5)該條語句的功能是什么?
}
}
fprintf(f2,"name\tyuwen\tshuxue\tyingyu\twuli\thuaxue\tzongfen\n ");
for(i=1;i=n;i++)
{
fprintf(f2,"%s\t",name[i]);
for(j=1;j=m;j++)
fprintf(f2,"%d\t",data[i][j]);
fprintf(f2,"%d\n",data[i][0]);
}
fclose(f1);// 6)問什么可以在這關閉輸入文件?
fclose(f2);
}
void search(char na[],char num){
int i=1,j;
while((i=n)(strcmp(na,name[i])!=0))i++;
if(i=n){
switch(num){
case '1':printf("%s的語文成績為%d!\n",na,data[i][1]);break;
case '2':printf("%s的數學成績為%d!\n",na,data[i][2]);break;
case '3':printf("%s的英語成績為%d!\n",na,data[i][3]);break;
case '4':printf("%s的物理成績為%d!\n",na,data[i][4]);break;
case '5':printf("%s的化學成績為%d!\n",na,data[i][5]);break;
case 'a':
printf("%s的各科成績為:\n",name[i]);
printf("語文\t數學\t英語\t物理\t化學\t總成績\n");
for(j=1;j=m;j++)
printf("%d\t",data[i][j]);
printf("%d\n",data[i][0]);
break;
default: printf("沒有這種查詢方式,請確認后再次輸入!\n");
}
}
else printf("查無此人,請確認后再次查詢!\n");
printf("\n\n");
}
main(){
init();//數據初始化
printf("數據初始化結束!\n\n");
printf("------------------------------------------------\n\n");
char na[20];
char num;
while(1){ // 7)該條語句的功能是什么?
//scanf("%s%c%c",na,num,num); // 8)此處的輸入可不可以用這條語句?
printf("請輸入學生的姓名和要查詢成績的科目代碼(中間用一個空格隔開):\n(1 語文 2 數學 3 英語 4 物理 5 化學 a 所有科目成績 q 退出)\n");
scanf("%s",na);
if(strcmp(na,"q")==0)break; // 9)問什么要用這條語句strcmp(na,"q")==0?
scanf("%c%c",num,num); // 10)為什么用了兩次num?
search(na,num);
printf("------------------------------------------------\n\n");
printf("\n請輸入新的查詢:\n\n");
}
system("pause");
}
稍改一下就行了!