使用sort函式對陣列 結構體排序

2021-10-03 13:33:38 字數 723 閱讀 8098

函式標頭檔案:#include

對陣列進行排序:sort(begin,end) / sort(begin,end,compare)

#include#includeusing namespace std;

bool compare(int a, int b)

int main()

; sort(a, a + 10, compare); //若要對陣列a進行公升序排序則直接使用sort(a,a+10)

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

return 0;

}

輸出結果將陣列a按降序排序:

對結構體進行排序:sort(begin,end,compare)

#include#includeusing namespace std;

typedef struct student s[3];

bool compare(student a, student b)

{ return a.num < b.num; //如果改為return a.num輸出結果將結構體s按num的公升序排列:

sort 排序(對陣列 結構體)

1 基本規則 sort 陣列起始指標,陣列尾指標,排序規則 note 標頭檔案是 include algorithm 陣列起始指標,陣列尾指標 左閉右開 排序規則 可省略,可自己寫 原理很簡單,掌握規則即可,下面舉幾個例子練習 2 對陣列排序 例1 int a sort a,a 7 預設 小 大 排...

使用Array的Sort 方法對陣列進行排序

來看看排序吧 using system using system.collections.generic using system.text namespace ljun csharp study 排序前的陣列 console.writeline 陣列排序前為 foreach int number ...

matble中建立結構體陣列 sort的排序

1 建立結構體陣列 struct在matlab中是用來建立結構體陣列的。通常有兩種用法 s struct field1 field2 這是建立乙個空的結構體,field1,field2是網域名稱稱 s struct field1 values1,field2 values2,這是在建立結構體的時候就...