STL中自定義排序的使用

2021-09-02 15:55:56 字數 3604 閱讀 9617

參考

[url]

[url]

[url]

[color=red][b][size=large]

強烈推薦文章

[url]

[b]如果要自己定義stl容器的元素類最好滿足stl容器對元素的要求[/b]

[color=red][b]必須要求:[/b][/color]

[quote]1、copy建構函式

2、賦值=操作符

3、能夠銷毀物件的析構函式[/quote]

[color=red][b]另外:[/b][/color]

[quote] 1、可用的預設建構函式,序列型容器必須,用於初始化元素

2、==操作符定義,用於判斷相等

3、《操作符定義,關聯型容器必須,用於預設排序[/quote]

你可在struct內加入 operator < ,就可以使struct有排序能力.因為而你的pcd struct內沒有指標,所以不須要有copy constructor和copy assignment, 編譯器會為你提供的, 你不須要自己做的.當你要排序時只要寫 sort( obj.begin(), obj.end() )就可.

下面是std::sort函式,有兩個版本:

template

void sort ( randomaccessiterator first, randomaccessiterator last );

template

void sort ( randomaccessiterator first, randomaccessiterator last, compare comp );

[b] sort函式有以下特徵:[/b]

[color=red]1. 要求輸入乙個範圍[first, last)

2. 隨機迭代器,能用此演算法的容器是支援隨機訪問的容器:vector, deque, string。

3.第乙個版本使用operator《進行比較,預設公升序排序,第二個版本使用comp做比較.

關於引數comp,comp帶兩個同型別的引數,如果第乙個引數排在第二個引數前面,返回true,否則返回false[/color]

它可以是函式指標,也可以是函式物件。函式指標好理解,何謂函式物件?

函式物件(function object),是過載了operator()函式的類(或結構體)例項化出來的物件,使用起來像函式,又叫仿函式。

stl本身提供了幾個比較函式,下面這些都是仿函式:

[quote] less(小於)

greater(大於)

equal_to(等於)

not_equal_to(不相等)

less_equal(小於等於)

greater_equal(大於等於)[/quote]

當容器元素為內建型別時可以使用,注意使用的格式,要加模版引數(由於是模板類)和後面的(),如下:

sort(vec.begin(), vec.end(), less());

下面看看這3種方法的實現:

// 排序元素,比較的物件

struct person

int id_;

string name_;

int age_;

};

// 方式1:過載operator《用於排序時的比較(寫在函式體內)

bool operator< (const person& rt)

// 排序函式寫法,預設呼叫operator<

sort(members.begin(), members.end());

// 方式2:寫比較函式

bool compage(const person& pl, const person& pr)

// 排序時傳入比較函式指標

sort(members.begin(), members.end(), compage);

// 方式3:仿函式

struct compname

};// 排序時傳入函式物件

sort(members.begin(), members.end(), compname());

[b]用函式物件代替函式指標的優點:[/b]

[color=red] 1. 函式物件可以儲存中間結果在資料成員中,而函式想要存中間結果須要設全域性變數或靜態變數,這個是我們不想要的。

2. 在函式物件中編譯器可以實現內聯呼叫,從而提公升效能。

[/color]

下面看乙個函式物件的擴充套件應用

// 利用函式物件實現公升降排序

struct compnameex

bool operator()(const person& pl, const person& pr)

private:

bool asce_;

};// 使用仿函式排序(公升降序)

sort(members.begin(), members.end(), compnameex(false));

c++中當 vector 中的資料型別為基本型別時我們呼叫std::sort函式很容易實現 vector中資料成員的公升序和降序排序,然而當vector中的資料型別為自定義結構體型別時,我們該怎樣實現公升序與降序排列呢?

[b]對運算符號進行過載[/b]

#include

#include

#include

using namespace std;

struct titem

bool operator >(const titem& rhs) const // 降序排序時必須寫的函式

};int main()

[b]全域性的比較函式[/b]

#include

#include

#include

using namespace std;

struct titem

;bool lessmark(const titem& stitem1, const titem& stitem2)

bool greatermark(const titem& stitem1, const titem& stitem2)

int main()

[b]函式物件[/b]

#include

#include

#include

using namespace std;

struct titem

;class compless

};class compgreater

};int main()

STL自定義排序小結

我們要先明白在哪個地方需要使用自定義排序 sort對vector容器排序時,第三個引數為乙個函式,函式內容即為排序的要求。1 當對可直接比較變數排序時,預設為公升序 include using namespace std 排序函式,自定義為降序 bool cmp2 const int a,const...

STL自定義比較器

struct person 自定義的比較器 struct comparebyage sort vec.begin vec.end comparebyage 排序傳入我們自定義的比較器map內部的實現使用的是樹,不能夠直接排序,我們可以將其放在乙個vector中,然後自定義乙個比較器去排序 map m...

STL 自定義比較器

說明 採用sort函式舉例 sort函式能夠接收2個形參 stl中的絕大多數用於查詢 排序的函式的前2個引數基本上都是乙個範圍 first,last 第3個引數一般是乙個比較器仿函式 即 設定大小比較原則compare 下面介紹5種常見的比較器定義手段 自定義普通比較器函式cmp include i...