日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ sort()函数的使用

發布時間:2024/2/28 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ sort()函数的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++ sort()函數

sort() 函數是C++排序方法之一,它的實現方式是基于快排的,所以時間復雜度為 O(nlogn),執行效率較高。?

sort() 函數的頭文件:#include <algorithm>

sort() 函數的參數:

(1)參數一是要排序的數組的起始地址。

(2)參數二是結束的地址(最后一位要排序的地址)。

(3)參數三是排序的方法,可以是從大到小也可是從小到大,若不寫第三個參數,則默認的排序方法是從小到大。

sort() 函數使用模板:sort(start, end, 排序方法);

?

默認方式排序

#include<iostream> #include<algorithm> using namespace std;int main() {int a[10] = {5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a,a+10);cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; }

?

根據排序需求自定義 compare()

bool compare(int a,int b) ?//從大到小 {return a > b; }bool compare(int a, int b) ?//絕對值排序 {return abs(a) > abs(b); } #include<iostream> #include<algorithm>using namespace std;bool compare(int a,int b) {return a>b; }int main() {int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, compare);//不需要對compare函數傳入參數cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; }

?

使用參數來確定排序方式

less<數據類型>() //從小到大排序 greater<數據類型>() //從大到小排序 #include<iostream> #include<algorithm> #include<functional> using namespace std;int main() {int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, less<int>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; } #include<iostream> #include<algorithm> #include<functional> using namespace std;int main() {int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, greater<int>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; }

?

對字符排序

#include<iostream> #include<algorithm> #include<functional> using namespace std;int main() {char a[11]="asdfghjklk";for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a,a+10,greater<char>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; }

?

對區間內的元素進行排序

#include <iostream> #include <algorithm> #include <vector> using namespace std;bool myfunction (int i,int j) { return (i<j); }//升序排列 bool myfunction2 (int i,int j) { return (i>j); }//降序排列struct myclass {bool operator() (int i,int j) { return (i<j);} } myobject;int main () {int myints[8] = {32,71,12,45,26,80,53,33};vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33//using default comparison (operator <):sort(myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33//using function as compsort(myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)//sort (myints,myints+8,myfunction);不用vector的用法//using object as compsort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)//print out content:cout << "myvector contains:";for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) //輸出cout << ' ' << *it;cout <<endl;return 0; }

?

string使用反向迭代器來完成逆序排列

#include <iostream> #include<algorithm> using namespace std;int main() {string str("qweasdzxc");string s(str.rbegin(),str.rend());cout << s <<endl;return 0; }

參考:https://blog.csdn.net/w_linux/article/details/76222112

https://baike.baidu.com/item/sort%E5%87%BD%E6%95%B0/11042699?fr=aladdin

?

總結

以上是生活随笔為你收集整理的C++ sort()函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。