C++ sort排序函數(shù)用法詳解
最近在刷ACM經(jīng)常用到排序,以前老是寫冒泡,可把冒泡帶到OJ里后發(fā)現(xiàn)經(jīng)常超時(shí),所以本想用快排,可是很多學(xué)長推薦用sort函數(shù),因?yàn)樽约簩懙目炫艑懖缓谜娴臎]有sort快,所以毅然決然選擇sort函數(shù)
用法
1、sort函數(shù)可以三個(gè)參數(shù)也可以兩個(gè)參數(shù),必須的頭文件#include < algorithm>和using namespace std;
2、它使用的排序方法是類似于快排的方法,時(shí)間復(fù)雜度為n*log2(n)
3、Sort函數(shù)有三個(gè)參數(shù):(第三個(gè)參數(shù)可不寫)
(1)第一個(gè)是要排序的數(shù)組的起始地址。
(2)第二個(gè)是結(jié)束的地址(最后一位要排序的地址)
(3)第三個(gè)參數(shù)是排序的方法,可以是從大到小也可是從小到大,還可以不寫第三個(gè)參數(shù),此時(shí)默認(rèn)的排序方法是從小到大排序。
兩個(gè)參數(shù)用法
#include <iostream>
#include <algorithm>
int main()
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++)
cout<<a[i]<<endl;
sort(a,a+20);
for(i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}輸出結(jié)果是升序排列。(兩個(gè)參數(shù)的sort默認(rèn)升序排序)
三個(gè)參數(shù)
// sort algorithm example
#include <iostream> ? ? // std::cout
#include <algorithm> ? ?// std::sort
#include <vector> ? ? ? // std::vector
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};
? std::vector<int> myvector (myints, myints+8); ? ? ? ? ? ? ? // 32 71 12 45 26 80 53 33
? // using default comparison (operator <):
? std::sort (myvector.begin(), myvector.begin()+4); ? ? ? ? ? //(12 32 45 71)26 80 53 33
? // using function as comp
? std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
? ? //std::sort (myints,myints+8,myfunction);不用vector的用法
? ??
? // using object as comp
? std::sort (myvector.begin(), myvector.end(), myobject); ? ? //(12 26 32 33 45 53 71 80)
? // print out content:
? std::cout << "myvector contains:";
? for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)//輸出
? ? std::cout << ' ' << *it;
? std::cout << '\n';
? return 0;
}string 使用反向迭代器來完成逆序排列
#include <iostream>
using namespace std;
int main()
{
string str("cvicses");
string s(str.rbegin(),str.rend());
cout << s <<endl;
return 0;
}
//輸出:sescivc
到此這篇關(guān)于C++ sort排序函數(shù)用法詳解的文章就介紹到這了,更多相關(guān)C++ sort排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++編程中__if_exists與__if_not_exists語句的用法
這篇文章主要介紹了C++編程中__if_exists與__if_not_exists語句的用法,是C++中用于判斷指定的標(biāo)識(shí)符是否存在的基礎(chǔ)的條件判斷語句,需要的朋友可以參考下2016-01-01
C++中volatile關(guān)鍵字及常見的誤解總結(jié)
這篇文章主要給大家介紹了關(guān)于C++中volatile關(guān)鍵字及常見的誤解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展
在C++中,定義函數(shù)時(shí)可以給形參指定一個(gè)默認(rèn)的值,這樣調(diào)用函數(shù)時(shí)如果沒有給這個(gè)形參賦值(沒有對(duì)應(yīng)的實(shí)參),那么就使用這個(gè)默認(rèn)的值。也就是說,調(diào)用函數(shù)時(shí)可以省略有默認(rèn)值的參數(shù)2022-04-04
C++實(shí)現(xiàn)比特幣系統(tǒng)的源碼
這篇文章主要介紹了C++實(shí)現(xiàn)比特幣系統(tǒng)的源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
詳解如何將Spire.Doc for C++集成到C++程序中
Spire.Doc for C++是一個(gè)專業(yè)的Word庫,供開發(fā)人員在任何類型的C++應(yīng)用程序中閱讀、創(chuàng)建、編輯、比較和轉(zhuǎn)換 Word 文檔,本文演示了如何以兩種不同的方式將 Spire.Doc for C++ 集成到您的 C++ 應(yīng)用程序中,希望對(duì)大家有所幫助2023-05-05
C++11 <future>中std::promise 介紹
這篇文章主要介紹了C++11 <future>中std::promise 介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

