C++中std::sort函數(shù)介紹和使用場景
std::sort是C++標(biāo)準(zhǔn)庫中的一個函數(shù),用于對容器中的元素進行排序。它可以按照默認的升序方式對元素進行排序,也可以指定自定義的比較函數(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
是一個可選的比較函數(shù),用于指定排序規(guī)則。如果不提供比較函數(shù),則默認按照升序排序。
使用場景
對數(shù)組進行排序
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}
對 vector 進行排序
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}
對字符串進行排序
std::string str = "hello world"; std::sort(str.begin(), str.end()); // 現(xiàn)在 str 字符串已經(jīng)按照字母表順序排列為 "dehllloorw"
對 pair 進行排序
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)按照第一個元素升序排列為 {{1, "banana"}, {2, "orange"}, {3, "apple"}}
完整示例
例子1: 對數(shù)組進行正向排序
#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: 對 vector 進行正向排序
#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: 對字符串進行正向排序
#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: 對 pair 進行正向排序
#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: 對數(shù)組進行反向排序
#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: 對 vector 進行反向排序
#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: 對字符串進行反向排序
#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)庫中常用的排序函數(shù)之一,它可以對各種類型的序列進行排序。通過指定不同的比較函數(shù),可以實現(xiàn)不同的排序方式。在實際開發(fā)中,我們經(jīng)常需要對數(shù)據(jù)進行排序以便進行后續(xù)處理或分析。掌握std::sort
函數(shù)的使用技巧可以幫助我們更高效地完成這些任務(wù)。
到此這篇關(guān)于C++中std::sort函數(shù)介紹和使用場景的文章就介紹到這了,更多相關(guān)C++ std::sort函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MATLAB實現(xiàn)五子棋游戲(雙人對戰(zhàn)、可悔棋)
這篇文章主要為大家詳細介紹了MATLAB實現(xiàn)五子棋游戲,可以進行雙人對戰(zhàn)、也可悔棋,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06VS2019調(diào)試C語言程序(監(jiān)視操作)的詳細步驟
在很多時候我們在寫程序的過程中會發(fā)現(xiàn)一些非編程錯誤的問題,這樣的問題很難直接分辨出來,但是我們可以用調(diào)試了一步一步的模擬程序運行的過程,來找出程序的錯誤,下面這篇文章主要給大家介紹了關(guān)于VS2019調(diào)試C語言程序(監(jiān)視操作)的詳細步驟,需要的朋友可以參考下2022-11-11