C++ 中的sort()排序函數用法


sort(first_pointer,first_pointer+n,cmp)

該函數可以給數組,或者鏈表list、向量排序。

實現原理:sort並不是簡單的快速排序,它對普通的快速排序進行了優化,此外,它還結合了插入排序和推排序。系統會根據你的數據形式和數據量自動選擇合適的排序方法,這並不是說它每次排序只選擇一種方法,它是在一次完整排序中不同的情況選用不同方法,比如給一個數據量較大的數組排序,開始采用快速排序,分段遞歸,分段之后每一段的數據量達到一個較小值后它就不繼續往下遞歸,而是選擇插入排序,如果遞歸的太深,他會選擇推排序。

此函數有3個參數:

參數1:第一個參數是數組的首地址,一般寫上數組名就可以,因為數組名是一個指針常量。

參數2:第二個參數相對較好理解,即首地址加上數組的長度n(代表尾地址的下一地址)。

參數3:默認可以不填,如果不填sort會默認按數組升序排序。也就是1,2,3,4排序。也可以自定義一個排序函數,改排序方式為降序什么的,也就是4,3,2,1這樣。

使用此函數需先包含:

#include <algorithm>
並且導出命名空間:

using namespace std;
簡單例子:對數組A的0~n-1元素進行升序排序,只要寫sort(A,A+n)即可;對於向量V也一樣,sort(v.begin(),v.end())即可。

自己編寫排序規則函數

例如:
bool compare(int a,int b)
{
return a<b; //升序排列,如果改為return a>b,則為降序

}
sort擴展

sort不只是能像上面那樣簡單的使用,我們可以對sort進行擴展,關鍵就在於第三個參數<cmp比較函數>,我們想降序排列,或者說我不是一個簡簡單單的數組,而是結構體、類怎么辦,下面給出一些方法和例子。

方法一:定義比較函數(最常用)
//情況一:數組排列
int A[100];
bool cmp1(int a,int b)//int為數組數據類型
{
return a>b;//降序排列
//return a<b;//默認的升序排列
}
sort(A,A+100,cmp1);

//情況二:結構體排序
Student Stu[100];
bool cmp2(Student a,Student b)
{
return a.id>b.id;//按照學號降序排列
//return a.id<b.id;//按照學號升序排列
}
sort(Stu,Stu+100,cmp2);
注:比較方法也可以放在結構體中或類中定義。

方法二:使用標准庫函數

另外,其實我們還可以再懶一點,在標准庫中已經有現成的。它在哪呢?答案是functional,我們include進來試試看。functional提供了一堆基於模板的比較函數對象,它們是:equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。這些東西的用法看名字就知道了。在這里,我么sort要用到的也只是greater和less就足夠了,用法如下:

● 升序:sort(begin,end,less<data-type>())

● 降序:sort(begin,end,greater<data-type>())

缺點:也只是實現簡單的排序,結構體不適用。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;
//簡單使用方法
sort(A,A+100,greater<int>());//降序排列
sort(A,A+100,less<int>());//升序排列
方法三:重載結構體或類的比較運算符

//情況一:在結構體內部重載
typedef struct Student{
int id;
string name;
double grade;

bool operator<(const Student& s)
{
return id>s.id;//降序排列
//return id<s.id;//升序排列
}
};
vector<Student> V;
sort(V.begin(),V.end());
//情況二:在外部重載
vector<Student> V;
bool operator<(const Student& s1, const Student& s2)
{
return s1.id>s2.id;//降序排列
//return s1.id<s2.id;//升序排列
}
sort(V.begin(),V.end());
注意:一定要重載<運算符,因為系統默認是降序,用的是<運算符。

方法四:聲明比較類(少用)

struct Less
{
bool operator()(const Student& s1, const Student& s2)
{
return s1.id<s2.id; //升序排列
}
};
sort(sutVector.begin(),stuVector.end(),Less());
一個list(鏈表)使用sort()實例:

#include "stdafx.h"
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
#include "stdlib.h"
#include <stdio.h>

using namespace std;

//給list起一個別名LISTINT
typedef list<int> LISTINT;
//再起一個別名 LISTCHAR
typedef list<int> LISTCHAR;

int _tmain(int argc, _TCHAR* argv[])
{

//用list容器處理整型數據
//用LISTINT創建一個名為listOne的list對象
LISTINT listOne;
//聲明i為迭代器
LISTINT::iterator i;

//從前面向listOne容器中添加數據
listOne.push_front (2);
listOne.push_front (1);

//從隊尾向listOne容器中添加數據

listOne.push_back (5);
listOne.push_back (4);
listOne.push_back (9);
listOne.push_back (7);
listOne.push_back (12);

//從前向后顯示listOne中的數據,排序前的鏈表
cout<<"listOne.begin()--- listOne.end():"<<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;

listOne.sort(); //用sort()函數排序,默認升序


//排序完畢后的列表
cout<<"listOne.begin()--- listOne.end():"<<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;


system("pause"); //按任意鍵后退出



return 0;
}
參考文獻:https://www.cnblogs.com/AlvinZH/p/6784862.html?utm_source=itdadao&utm_medium=referral

參考文獻:https://www.cnblogs.com/luorende/p/6121906.html
---------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM