STLset容器排序

2021-10-21 17:32:24 字數 1049 閱讀 8837

學習目標:

set容器預設排序規則為從小到大,掌握如何改變排序規則

主要技術點:

利用仿函式,可以改變排序規則

**示例一:

#include

#include

using

namespace std;

//set容器排序

class

mycompare};

void

test01()

cout << endl;

//指定排序規則為從小到大

set<

int, mycompare>s2;

s2.insert(10

);s2.

insert(40

);s2.

insert(50

);s2.

insert(30

);s2.

insert(20

);for(set<

int, mycompare>

::iterator it = s2.

begin()

; it != s2.

end(

); it++

) cout << endl;

}int

main()

總結:利用仿函式可以指定set容器的排序規則

**示例二:

#include

#include

#include

using

namespace std;

//set容器排序,存放自定義資料型別

class

person

string m_name;

int m_age;};

class

comperson};

void

test01()

}int

main()

總結:

對於自定義資料型別,set必須指定排序規則才可以插入資料

STL set容器常用API

set容器,容器內部將資料自動排序 平衡二叉樹 不能插入重複元素。multiset可以插入重複元素。不能修改容器中的值,通過刪除值,在插入。define crt secure no warnings include include include include using namespace st...

STL set集合容器常用用法

set集合容器 實現了紅黑樹的平衡二叉檢索樹的資料結構,插入元素時,它會自動調整二叉樹的排列,把元素放到適當的位置,以保證每個子樹根節點鍵值大於左子樹所有節點的鍵值,小於右子樹所有節點的鍵值 另外,還得保證根節點左子樹的高度與右子樹高度相等。平衡二叉檢索樹使用中序遍歷演算法,檢索效率高於vector...

STL set容器的一點總結

整理了一下set常用語句,參看這篇 常用語句 include 包含set的標頭檔案 setp 定義乙個集合容器,不包含重複元素 multisetp 定義乙個集合容器,包含重複元素 p.insert x 將x元素加入集合中 p.empty 判斷集合是否為空 p.clear 清除集合中所有的元素 p.c...