set集合容器

2021-09-27 11:01:31 字數 1633 閱讀 2471

#include

#include

#include

using

namespace std;

//自定義比較函式 mycomp 過載"()"操作符

struct mycomp

else}}

;struct info};

intmain()

cout << endl;

//元素的反向遍歷

set<

int>

::reverse_iterator rit;

//定義反向迭代器

for(rit=s.

rbegin()

;rit!=s.

rend()

;rit++

) cout << endl;

//元素的刪除

s.erase(9

);//刪除鍵值為9的那個元素

for(it=s.

begin()

;it!=s.

end(

);it++

) cout << endl;

// s.clear();//清空集合

// cout << s.size() << endl;

//元素的檢索

it=s.

find(8

);//查詢鍵值為8的元素

if(it!=s.

end())

else

//自定義比較函式

set<

int,mycomp> a;

a.insert(5

);a.insert(8

);a.insert(7

);a.insert(8

);//重複 無效

a.insert(2

);set<

int,mycomp>

::iterator i;

//定義前向迭代器

for(i=a.

begin()

;i!=a.

end(

);i++

) cout << endl;

//元素是結構體 直接把比較函式寫在結構體內

set f;

info info;

//插入3個元素

info.name=

"john"

; info.score=

80.5

; f.

insert

(info);

info.name=

"jake"

; info.score=

90.5

; f.

insert

(info);

info.name=

"tom"

; info.score=

65.3

; f.

insert

(info);

set::iterator j;

//定義前向迭代器

for(j=f.

begin()

;j!=f.

end(

);j++

) cout << endl;

return0;

}

set 集合容器

簡單學習一下set集合容器 標頭檔案 include using namespace std sets 必須是有定義 運算子的型別 int,string 向s中加資料 s.insert elem 插入elem之後元素預設按公升序排序,集合中是沒有重複元素的,每個不同的元素只存乙個 s.clear 清...

set集合容器

set集合容器 set的定義 set容器的標頭檔案 include 定義 set 鍵值 s set的優缺 1.set容器的檢索使用中序遍歷演算法,檢索效率高於vector deque list等容器。2.set容器會對插入的元素按照鍵值由小到大的順序排序。3.set集合的主要目的就是為了快速檢索。4...

set集合容器

set集合容器 呼叫標頭檔案 includeusing namespace std 詳細用法 部分 sett 定義乙個int型別的容器,注意set裡的每個元素只會出現1次 t.insert k 插入元素k,多次插入同乙個元素後面無效 t.count k 判斷元素k是否在容器內 t.erase k 刪...