STL常用算法之排序算法詳解
STL常用算法排序算法
1、sort()
- sort():對容器或普通數(shù)組中范圍內(nèi)的元素進(jìn)行排序,默認(rèn)進(jìn)行升序排序,也可以自定義排序規(guī)則。
- sort() 函數(shù)只對 array、vector、deque 這 3 個容器提供支持。
- sort() 函數(shù)在對自定義的類對象實(shí)現(xiàn)排序時,需要在該類的內(nèi)部提供移動構(gòu)造函數(shù)和移動賦值運(yùn)算符。
函數(shù)原型:該函數(shù)有以下兩種語法格式
//對 [beg, end) 區(qū)域內(nèi)的元素做默認(rèn)的升序排序 sort (iterator beg,iterator end); //按照指定的 comp 排序規(guī)則,對 [beg, end) 區(qū)域內(nèi)的元素進(jìn)行排序 sort (iterator beg,iterator end, Compare comp);
參數(shù)說明:
- beg 開始迭代器
- end 結(jié)束迭代器
- comp 標(biāo)準(zhǔn)庫提供的排序規(guī)則(如 greater<T>);或普通函數(shù)以及函數(shù)對象接收的自定義的排序規(guī)則
默認(rèn)排序和標(biāo)準(zhǔn)庫提供的排序
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v{1,4,5,3,2};
//默認(rèn)升序排序
sort(v.begin(), v.end());//1 2 3 4 5
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//從大到小排序,標(biāo)準(zhǔn)庫提供的排序規(guī)則
sort(v.begin(), v.end(), greater<int>());//5 4 3 2 1
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
自定義排序規(guī)則
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void myPrint(int val) {
cout << val << " ";
}
//以普通函數(shù)的方式實(shí)現(xiàn)自定義排序規(guī)則(升序)
bool mycomp(int i, int j) {
return (i < j);
}
//以函數(shù)對象的方式實(shí)現(xiàn)自定義排序規(guī)則(升序)
class mycomp2 {
public:
bool operator() (int i, int j) {
return (i < j);
}
};
void test01() {
vector<int> v{ 32, 71, 12, 45, 26, 80, 53, 33 };
sort(v.begin(), v.end(), mycomp);//12 26 32 33 45 53 71 80
for_each(v.begin(), v.end(), myPrint);
cout << endl;
sort(v.begin(), v.end(), mycomp2());//12 26 32 33 45 53 71 80
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
2、random_shuffle()
random_shuffle():指定范圍內(nèi)的元素隨機(jī)調(diào)整次序
函數(shù)原型:
random_suffle(iterator beg,iterator end);
參數(shù)說明:
- beg 開始迭代器
- end 結(jié)束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//打亂順序前的遍歷
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//打亂順序后的遍歷
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
//加入隨機(jī)數(shù)種子,使每次程序啟動的隨機(jī)數(shù)都不一樣
srand((unsigned int)time(NULL));
test01();
system("pause");
return 0;
}

3、merge()
合并排序,merge() 函數(shù)用于將 2 個有序序列合并為 1 個有序容器,前提是這 2 個有序容器的排序規(guī)則相同(要么都是升序,要么都是降序)。并且最終借助該函數(shù)獲得的新有序容器,其排序規(guī)則也和這 2 個有序容器要相同。
函數(shù)原型:該函數(shù)有以下兩種格式
//默認(rèn)升序?yàn)榕判蛞?guī)則,[beg1, end1) 和 [beg2, end2) 指定區(qū)域內(nèi)的元素必須支持 < 小于運(yùn)算符 merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest); //自定義的 comp 規(guī)則作為排序規(guī)則,[beg1, end1) 和 [beg2, end2) 指定區(qū)域內(nèi)的元素必須支持comp 排序規(guī)則內(nèi)的比較運(yùn)算符 merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest, Compare comp);
參數(shù)說明:
- beg1 容器1開始迭代器
- end1 容器1結(jié)束迭代器
- beg2 容器2開始迭代器
- end2 容器2結(jié)束迭代器
- dest 目標(biāo)容器開始迭代器,為最終生成的新有序序列指定存儲位置
- comp 用于自定義排序規(guī)則
默認(rèn)排序
void myPrint(int val) {
cout << val << " ";
}
void test01() {
//有序的容器
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i * 2);
}
//新有序容器
vector<int> v3;
//一定要提前給容器分配空間
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
//打印出來的還是個有序序列
for_each(v3.begin(), v3.end(), myPrint);//0 0 1 2 2 3 4 4 5 6 6 7 8 8 9 10 12 14 16 18
cout << endl;
}自定義排序規(guī)則
void myPrint(int val) {
cout << val << " ";
}
//以普通函數(shù)的方式實(shí)現(xiàn)自定義排序規(guī)則(降序)
bool mycomp(int i, int j) {
return (i > j);
}
//以函數(shù)對象的方式實(shí)現(xiàn)自定義排序規(guī)則(降序)
class mycomp2 {
public:
bool operator() (int i, int j) {
return (i > j);
}
};
void test01() {
//有序的容器
vector<int> v1;
vector<int> v2;
for (int i = 10; i > 0; i--) {
v1.push_back(i);
v2.push_back(i * 2);
}
//新有序容器
vector<int> v3;
//一定要提前給容器分配空間
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(), mycomp);
for_each(v3.begin(), v3.end(), myPrint);//20 18 16 14 12 10 10 9 8 8 7 6 6 5 4 4 3 2 2 1
cout << endl;
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(), mycomp2());
for_each(v3.begin(), v3.end(), myPrint);//20 18 16 14 12 10 10 9 8 8 7 6 6 5 4 4 3 2 2 1
cout << endl;
}
4、reverse()
reverse()函數(shù):將容器指定范圍內(nèi)的元素進(jìn)行反轉(zhuǎn)
函數(shù)原型:
reverse(iterator beg,iterator end);
參數(shù)說明:
- beg 開始迭代器
- end 結(jié)束迭代器
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
//有序的容器
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), myPrint);//9 8 7 6 5 4 3 2 1 0
cout << endl;
}到此這篇關(guān)于STL常用算法之排序算法詳解的文章就介紹到這了,更多相關(guān)STL排序算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++淺析鄰接表拓?fù)渑判蛩惴ǖ膶?shí)現(xiàn)
這篇文章主要介紹了C/C++對于鄰接表拓?fù)渑判蛩惴ǖ膶?shí)現(xiàn),鄰接表是圖的一種鏈?zhǔn)酱鎯Ψ椒?,其?shù)據(jù)結(jié)構(gòu)包括兩部分:節(jié)點(diǎn)和鄰接點(diǎn)2022-07-07
C語言 function recursion函數(shù)遞歸詳解
遞歸指的是在函數(shù)的定義中使用函數(shù)自身的方法,舉個例子: 從前有座山,山里有座廟,廟里有個老和尚,正在給小和尚講故事呢!故事是什么呢?"從前有座山,山里有座廟,廟里有個老和尚,正在給小和尚講故事呢!故事是什么呢?"從前有座山,山里有座廟,循環(huán)下去2021-10-10
C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)
C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程
這篇文章主要介紹了C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01

