網(wǎng)站首頁 編程語言 正文
1. 引言
在C++中,sort()函數(shù)常常用來對容器內(nèi)的元素進行排序,先來了解一下sort()函數(shù)。
sort()函數(shù)有三個參數(shù):
- 第一個是要排序的容器的起始迭代器。
- 第二個是要排序的容器的結(jié)束迭代器。
- 第三個參數(shù)是排序的方法,是可選的參數(shù)。默認的排序方法是從小到大排序,也就是
less<Type>()
,還提供了greater<Type>()
進行從大到小排序。這個參數(shù)的類型是函數(shù)指針
,less和greater
實際上都是類/結(jié)構(gòu)體
,內(nèi)部分別重載了()運算符
,稱為仿函數(shù),所以實際上less<Type>()和greater<Type>()
都是函數(shù)名,也就是函數(shù)指針。我們還可以用普通函數(shù)來定義排序方法。
如果容器內(nèi)元素的類型是內(nèi)置類型或string類型
,我們可以直接用less<Type>()或greater<Type>()
進行排序。但是如果數(shù)據(jù)類型是我們自定義的結(jié)構(gòu)體或者類的話,我們需要自定義排序函數(shù),
有三種寫法:
-
重載 < 或 > 運算符:重載
<
運算符,傳入less<Type>()
進行升序排列。重載>
運算符,傳入greater<Type>()
進行降序排列。這種方法只能針對一個維度排序,不靈活。 -
普通函數(shù):寫普通函數(shù)cmp,傳入
cmp
按照指定規(guī)則排列。這種方法可以對多個維度排序,更靈活。 -
仿函數(shù):寫仿函數(shù)cmp,傳入
cmp<Type>()
按照指定規(guī)則排列。這種方法可以對多個維度排序,更靈活。
2. 自定義排序規(guī)則
2.1 重寫 < 或 > 運算符
#include <bits/stdc++.h> using namespace std; struct Person { int id; int age; Person(int id,int age):id(id),age(age){} //重載<運算符,進行升序排列 bool operator < (const Person& p2) const { return id < p2.id; } //重載>運算符,進行降序排列 bool operator > (const Person& p2) const { return id > p2.id; } }; int main() { Person p1(1, 10), p2(2, 20), p3(3, 30); vector<Person> ps; ps.push_back(p2); ps.push_back(p1); ps.push_back(p3); sort(ps.begin(), ps.end(), less<Person>()); for (int i = 0; i < 3; i++) { cout << ps[i].id << " " << ps[i].age << endl; } cout << endl; sort(ps.begin(), ps.end(), greater<Person>()); for (int i = 0; i < 3; i++) { cout << ps[i].id << " " << ps[i].age << endl; } cout << endl; }
2.2 普通函數(shù)
#include <bits/stdc++.h> using namespace std; struct Person { int id; int age; Person(int id,int age):id(id),age(age){} }; //普通函數(shù) bool cmp(const Person& p1, const Person& p2) { if (p1.id == p2.id) return p1.age >= p2.age; return p1.id < p2.id; } int main() { Person p1(1, 10), p2(2, 20), p3(3, 30), p4(3, 40); vector<Person> ps; ps.push_back(p2); ps.push_back(p1); ps.push_back(p3); ps.push_back(p4); sort(ps.begin(), ps.end(), cmp);//傳入函數(shù)指針cmp for (int i = 0; i < 4; i++) { cout << ps[i].id << " " << ps[i].age << endl; } }
2.3 仿函數(shù)
#include <bits/stdc++.h> using namespace std; struct Person { int id; int age; Person(int id, int age) :id(id), age(age) {} }; //仿函數(shù) struct cmp { bool operator()(const Person& p1, const Person& p2) { if (p1.id == p2.id) return p1.age >= p2.age; return p1.id < p2.id; } }; int main() { Person p1(1, 10), p2(2, 20), p3(3, 30), p4(3, 40); vector<Person> ps; ps.push_back(p2); ps.push_back(p1); ps.push_back(p3); ps.push_back(p4); sort(ps.begin(), ps.end(), cmp()); //傳入函數(shù)指針cmp() for (int i = 0; i < 4; i++) { cout << ps[i].id << " " << ps[i].age << endl; } }
原文鏈接:https://blog.csdn.net/qq_42676511/article/details/126955901
相關(guān)推薦
- 2023-05-31 Hadoop腳本遠程控制中SSH常見問題詳解_服務(wù)器其它
- 2022-06-29 python人工智能tensorflow構(gòu)建循環(huán)神經(jīng)網(wǎng)絡(luò)RNN_python
- 2022-07-21 提高新手寫代碼效率的Emmet插件怎么使用
- 2022-12-04 Android自定義View繪制貝塞爾曲線實現(xiàn)流程_Android
- 2022-06-20 關(guān)于Golang獲取當前項目絕對路徑的問題_Golang
- 2022-04-17 論一次 taro小程序分包優(yōu)化經(jīng)歷,小程序體積過大的優(yōu)化
- 2022-05-26 openwrt安裝docker并啟動的操作方法_docker
- 2022-03-31 用python實現(xiàn)彈球小游戲_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支