C++ sort()函数的使用
生活随笔
收集整理的這篇文章主要介紹了
C++ sort()函数的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++ sort()函數(shù)
sort() 函數(shù)是C++排序方法之一,它的實現(xiàn)方式是基于快排的,所以時間復雜度為 O(nlogn),執(zhí)行效率較高。?
sort() 函數(shù)的頭文件:#include <algorithm>
sort() 函數(shù)的參數(shù):
(1)參數(shù)一是要排序的數(shù)組的起始地址。
(2)參數(shù)二是結(jié)束的地址(最后一位要排序的地址)。
(3)參數(shù)三是排序的方法,可以是從大到小也可是從小到大,若不寫第三個參數(shù),則默認的排序方法是從小到大。
sort() 函數(shù)使用模板: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; }?
根據(jù)排序需求自定義 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函數(shù)傳入?yún)?shù)cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0; }?
使用參數(shù)來確定排序方式
less<數(shù)據(jù)類型>() //從小到大排序 greater<數(shù)據(jù)類型>() //從大到小排序 #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; }?
對區(qū)間內(nèi)的元素進行排序
#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
?
總結(jié)
以上是生活随笔為你收集整理的C++ sort()函数的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看图学习区块链概念
- 下一篇: 使用海康威视设备在Web端显示实时视频