c++自定義sort()函數(shù)的排序方法介紹
1. 引言
在C++中,sort()函數(shù)常常用來對(duì)容器內(nèi)的元素進(jìn)行排序,先來了解一下sort()函數(shù)。
sort()函數(shù)有三個(gè)參數(shù):
- 第一個(gè)是要排序的容器的起始迭代器。
- 第二個(gè)是要排序的容器的結(jié)束迭代器。
- 第三個(gè)參數(shù)是排序的方法,是可選的參數(shù)。默認(rèn)的排序方法是從小到大排序,也就是
less<Type>()
,還提供了greater<Type>()
進(jìn)行從大到小排序。這個(gè)參數(shù)的類型是函數(shù)指針
,less和greater
實(shí)際上都是類/結(jié)構(gòu)體
,內(nèi)部分別重載了()運(yùn)算符
,稱為仿函數(shù),所以實(shí)際上less<Type>()和greater<Type>()
都是函數(shù)名,也就是函數(shù)指針。我們還可以用普通函數(shù)來定義排序方法。
如果容器內(nèi)元素的類型是內(nèi)置類型或string類型
,我們可以直接用less<Type>()或greater<Type>()
進(jìn)行排序。但是如果數(shù)據(jù)類型是我們自定義的結(jié)構(gòu)體或者類的話,我們需要自定義排序函數(shù),
有三種寫法:
- 重載 < 或 > 運(yùn)算符:重載
<
運(yùn)算符,傳入less<Type>()
進(jìn)行升序排列。重載>
運(yùn)算符,傳入greater<Type>()
進(jìn)行降序排列。這種方法只能針對(duì)一個(gè)維度排序,不靈活。 - 普通函數(shù):寫普通函數(shù)cmp,傳入
cmp
按照指定規(guī)則排列。這種方法可以對(duì)多個(gè)維度排序,更靈活。 - 仿函數(shù):寫仿函數(shù)cmp,傳入
cmp<Type>()
按照指定規(guī)則排列。這種方法可以對(duì)多個(gè)維度排序,更靈活。
2. 自定義排序規(guī)則
2.1 重寫 < 或 > 運(yùn)算符
#include <bits/stdc++.h> using namespace std; struct Person { int id; int age; Person(int id,int age):id(id),age(age){} //重載<運(yùn)算符,進(jìn)行升序排列 bool operator < (const Person& p2) const { return id < p2.id; } //重載>運(yùn)算符,進(jìn)行降序排列 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; } }
到此這篇關(guān)于c++自定義sort()函數(shù)的排序方法介紹的文章就介紹到這了,更多相關(guān)c++ sort排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++寬窄字符轉(zhuǎn)換與輸出的多種實(shí)現(xiàn)方法
本文主要介紹了C/C++寬窄字符轉(zhuǎn)換與輸出的多種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08C++中的取余函數(shù)remainder與fmod詳解
這篇文章主要為大家詳細(xì)介紹了C++中的取余函數(shù)remainder、fmod的具體使用以及自編的remainder及fmod,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2023-05-05C語言中位運(yùn)算符"|"的5種高級(jí)用法總結(jié)
這篇文章主要為大家詳細(xì)介紹了C語言中位運(yùn)算符"|"的5種高級(jí)用法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2023-04-04