C++中std::sort函數(shù)介紹和使用場(chǎng)景
std::sort是C++標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù),用于對(duì)容器中的元素進(jìn)行排序。它可以按照默認(rèn)的升序方式對(duì)元素進(jìn)行排序,也可以指定自定義的比較函數(shù)來(lái)實(shí)現(xiàn)降序排序等其他排序方式。
函數(shù)介紹
std::sort函數(shù)位于<algorithm>頭文件中,其原型如下:
template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
其中,first和last表示待排序序列的起始和結(jié)束迭代器,comp是一個(gè)可選的比較函數(shù),用于指定排序規(guī)則。如果不提供比較函數(shù),則默認(rèn)按照升序排序。
使用場(chǎng)景
對(duì)數(shù)組進(jìn)行排序
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 現(xiàn)在 arr 數(shù)組已經(jīng)按照升序排列為 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
對(duì) vector 進(jìn)行排序
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(v.begin(), v.end());
// 現(xiàn)在 v 向量已經(jīng)按照升序排列為 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
對(duì)字符串進(jìn)行排序
std::string str = "hello world"; std::sort(str.begin(), str.end()); // 現(xiàn)在 str 字符串已經(jīng)按照字母表順序排列為 "dehllloorw"
對(duì) pair 進(jìn)行排序
std::pair<int, std::string> p1 = {3, "apple"};
std::pair<int, std::string> p2 = {1, "banana"};
std::pair<int, std::string> p3 = {2, "orange"};
std::vector<std::pair<int, std::string>> vec = {p1, p2, p3};
std::sort(vec.begin(), vec.end());
// 現(xiàn)在 vec 向量已經(jīng)按照第一個(gè)元素升序排列為 {{1, "banana"}, {2, "orange"}, {3, "apple"}}
完整示例
例子1: 對(duì)數(shù)組進(jìn)行正向排序
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
cout << "Sorted array is: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
輸出結(jié)果為:Sorted array is: 1 1 2 3 3 4 5 5 5 6 9
例子2: 對(duì) vector 進(jìn)行正向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(v.begin(), v.end());
cout << "Sorted vector is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
輸出結(jié)果為:Sorted vector is: 1 1 2 3 3 4 5 5 5 6 9
例子3: 對(duì)字符串進(jìn)行正向排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "hello world";
sort(str.begin(), str.end());
cout << "Sorted string is: " << str << endl;
return 0;
}
輸出結(jié)果為:Sorted string is: dehllloorw
例子4: 對(duì) pair 進(jìn)行正向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(const pair<int, string>& a, const pair<int, string>& b) {
return a.first < b.first;
}
int main() {
vector<pair<int, string>> v = {{3, "apple"}, {1, "banana"}, {2, "orange"}};
sort(v.begin(), v.end(), compare);
cout << "Sorted vector of pairs is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << " ";
}
cout << endl;
return 0;
}
輸出結(jié)果為:Sorted vector of pairs is: 1 banana 2 orange 3 apple
例子5: 對(duì)數(shù)組進(jìn)行反向排序
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n, greater<int>());
cout << "Reverse sorted array is: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
輸出結(jié)果為:Reverse sorted array is: 9 6 5 5 5 4 3 3 2 1 1
例子6: 對(duì) vector 進(jìn)行反向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(v.rbegin(), v.rend(), greater<int>());
cout << "Reverse sorted vector is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
輸出結(jié)果為:Reverse sorted vector is: 9 6 5 5 5 4 3 3 2 1 1
例子7: 對(duì)字符串進(jìn)行反向排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool compare(const string& a, const string& b) {
return a > b;
}
int main() {
string str = "hello world";
sort(str.rbegin(), str.rend(), compare);
cout << "Reverse sorted string is: " << str << endl;
return 0;
}
輸出結(jié)果為:Reverse sorted string is: dlrow olleh
總結(jié)
std::sort函數(shù)是C++標(biāo)準(zhǔn)庫(kù)中常用的排序函數(shù)之一,它可以對(duì)各種類(lèi)型的序列進(jìn)行排序。通過(guò)指定不同的比較函數(shù),可以實(shí)現(xiàn)不同的排序方式。在實(shí)際開(kāi)發(fā)中,我們經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行排序以便進(jìn)行后續(xù)處理或分析。掌握std::sort函數(shù)的使用技巧可以幫助我們更高效地完成這些任務(wù)。
到此這篇關(guān)于C++中std::sort函數(shù)介紹和使用場(chǎng)景的文章就介紹到這了,更多相關(guān)C++ std::sort函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于C++內(nèi)部類(lèi)的介紹與使用示例
今天小編就為大家分享一篇關(guān)于關(guān)于C++內(nèi)部類(lèi)的介紹與使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
解析C語(yǔ)言中位字段內(nèi)存分配的問(wèn)題
本篇文章是對(duì)C語(yǔ)言中位字段內(nèi)存分配的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
boost.asio框架系列之調(diào)度器io_service
這篇文章介紹了boost.asio框架系列之調(diào)度器io_service,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出,是數(shù)據(jù)結(jié)構(gòu)與算法中比較基礎(chǔ)的重要內(nèi)容,有必要加以牢固掌握,需要的朋友可以參考下2014-08-08
C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之單精度浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)
在C語(yǔ)言中,單精度浮點(diǎn)數(shù)(float)和雙精度浮點(diǎn)數(shù)(double)類(lèi)型都是用來(lái)儲(chǔ)存實(shí)數(shù)的,雙精度是用記憶較多,有效數(shù)字較多,數(shù)值范圍較大。2017-10-10

