C++?sort()與stable_sort()使用指北(附示例代碼)
在 C++ 標(biāo)準(zhǔn)庫(kù)中,std::sort() 和 std::stable_sort() 都用于對(duì)容器中的元素進(jìn)行排序,但二者最根本的區(qū)別在于穩(wěn)定性。
1、排序的穩(wěn)定性是個(gè)什么玩意
如果兩個(gè)元素相等(比較結(jié)果為等價(jià)),排序后它們的相對(duì)順序與原序列中保持一致。
2、到底誰(shuí)更穩(wěn)定
- std::sort() 是不穩(wěn)定的排序算法,意味著相等元素的相對(duì)順序在排序后可能被改變。
- std::stable_sort() 是穩(wěn)定排序,保證相等元素的原始輸入順序在排序后保持不變。
3、它們內(nèi)部的實(shí)現(xiàn)方式
- std::sort() 通常采用Introsort(內(nèi)省排序),結(jié)合快速排序、堆排序和插入排序,平均性能極佳。
- std::stable_sort() 多基于歸并排序(Merge Sort),因其天然具備穩(wěn)定性,適合分治策略下的有序合并。
盡管 stable_sort() 提供了穩(wěn)定性保障,但其代價(jià)是更高的內(nèi)存消耗和潛在的性能下降(尤其在大數(shù)據(jù)集上)。對(duì)于金融系統(tǒng)、考試排名、事件日志等場(chǎng)景,穩(wěn)定性是硬性需求,應(yīng)無(wú)條件選用 stable_sort()。
4、小結(jié)
- std::sort:更快、更省內(nèi)存,但不保證穩(wěn)定性。
- std::stable_sort:稍慢、更耗內(nèi)存,但保證穩(wěn)定性。
- 一句話:性能優(yōu)先用 sort,順序敏感用 stable_sort。
- 備注:對(duì)于頻繁排序的小型容器,可考慮使用 std::list::sort()
5、示例代碼
#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <algorithm>//sort
using std::vector;
using std::list;
using std::string;
struct Student
{
string name;
double score;
Student(const string &n, double s) : name(n), score(s) {}
// 重載 operator< 以按score升序排序(list::sort)
bool operator<(const Student& other) const {
return score < other.score;
}
};
bool CompareByScore(const Student& a, const Student& b) {
return a.score > b.score; // 降序
}
//
bool CompareStudent(const Student& a, const Student& b) {
if (a.score != b.score){
return a.score < b.score;
}
return a.name < b.name; // 成績(jī)相同時(shí)按名字升序
}
int main(int argc, char *argv[])
{
std::vector<Student> studentArray = {
{"Candy", 91.0},
{"Body", 91.0},
{"Andy", 91.0},
{"Lucy", 91.0},
{"Lily", 90.5},
{"Luck", 92.5},
{"Kandy", 95.0},
};
do{
std::cout << "v1: std::sort" << std::endl;
auto v1 = studentArray;
std::sort(v1.begin(), v1.end(), [](const Student &a, const Student &b){
return a.score > b.score;//降序
});
for (const auto& s : v1) {
std::cout << s.name << ": " << s.score << "\n";
}
}while(false);
do{
// 使用 stable_sort 保證同分學(xué)生順序不變
std::cout << "\nv2: std::stable_sort" << std::endl;
auto v2 = studentArray;
std::stable_sort(v2.begin(), v2.end(), CompareByScore);
for (const auto& s : v2) {
std::cout << s.name << ": " << s.score << "\n";
}
}while(false);
do{
// 對(duì)于頻繁排序的小型容器,可考慮使用 std::list::sort()(穩(wěn)定且鏈表友好)
std::list<Student> studentList;
for (const auto& s : studentArray){
studentList.push_back(s);
}
// 使用 std::list::sort() 進(jìn)行排序
std::cout << "\nlist1: sort" << std::endl;
auto list1 = studentList;
list1.sort(); // 使用 operator<
for (const auto& s : list1) {
std::cout << s.name << ": " << s.score << "\n";
}
//使用自定義比較函數(shù)
std::cout << "\nlist2: CompareStudent" << std::endl;
auto list2 = studentList;
list2.sort(CompareStudent);
for (const auto& s : list2) {
std::cout << s.name << ": " << s.score << "\n";
}
}while(false);
return 0;
}
總結(jié)
到此這篇關(guān)于C++ sort()與stable_sort()使用的文章就介紹到這了,更多相關(guān)C++ sort()與stable_sort()使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解C語(yǔ)言二分查找的簡(jiǎn)單應(yīng)用
這篇文章主要介紹了二分查找算法在C語(yǔ)言程序中的使用示例,文中最后提到了使用二分查找法一個(gè)需要注意的地方,需要的朋友可以參考下2021-08-08
探討:C++中函數(shù)返回引用的注意事項(xiàng)
本篇文章是對(duì)C++中函數(shù)返回引用的注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++中的不規(guī)則二維數(shù)組實(shí)現(xiàn)代碼
本文介紹了一個(gè)在C++中保存不定長(zhǎng)二維數(shù)組的數(shù)據(jù)結(jié)構(gòu),在這個(gè)結(jié)構(gòu)中,我們使用了一個(gè)含有指針和數(shù)組長(zhǎng)度的結(jié)構(gòu)體,用這樣的一個(gè)結(jié)構(gòu)體構(gòu)造一個(gè)結(jié)構(gòu)體數(shù)組,用于存儲(chǔ)每一個(gè)不定長(zhǎng)的數(shù)組,感興趣的朋友一起看看吧2024-03-03
C語(yǔ)言Easyx實(shí)現(xiàn)貪吃蛇詳解
這篇文章主要為大家詳細(xì)介紹了基于easyx的C++實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C語(yǔ)言實(shí)現(xiàn)用?*?打印X形圖案
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)用?*?打印X形圖案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

