網站首頁 編程語言 正文
最近在刷ACM經常用到排序,以前老是寫冒泡,可把冒泡帶到OJ里后發現經常超時,所以本想用快排,可是很多學長推薦用sort函數,因為自己寫的快排寫不好真的沒有sort快,所以毅然決然選擇sort函數
用法
1、sort函數可以三個參數也可以兩個參數,必須的頭文件#include < algorithm>和using namespace std;
2、它使用的排序方法是類似于快排的方法,時間復雜度為n*log2(n)
3、Sort函數有三個參數:(第三個參數可不寫)
(1)第一個是要排序的數組的起始地址。
(2)第二個是結束的地址(最后一位要排序的地址)
(3)第三個參數是排序的方法,可以是從大到小也可是從小到大,還可以不寫第三個參數,此時默認的排序方法是從小到大排序。
兩個參數用法
#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;
}
輸出結果是升序排列。(兩個參數的sort默認升序排序)
三個參數
// 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
原文鏈接:https://blog.csdn.net/w_linux/article/details/76222112
相關推薦
- 2022-02-13 Flutter在showModalBottomSheet中使用StatefulWidget
- 2022-10-02 C#使用is、as關鍵字以及顯式強轉實現引用類型轉換_C#教程
- 2023-05-19 Python基于TensorFlow接口實現深度學習神經網絡回歸_python
- 2022-06-22 Android使用EventBus多次接收消息_Android
- 2023-01-27 React?useEffect的理解與使用_React
- 2022-08-08 pd.to_datetime中時間object轉換datetime實例_python
- 2023-05-03 Pycharm?Terminal?與Project?interpreter?安裝包不同步問題解決_p
- 2021-11-10 Android?Studio設置繪制布局時的視圖_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支