建立set對象
爲了管理set的二叉樹鏈表數據,先用set容器的構造函數,建立一個set對象
(1) set()
用默認的less<T>函數對象和內存分配器,建立一個沒有任何數據元素的set對象。
set<int> s; //建立了空的set對象s,元素類型爲整型int;
(2) set(const key_compare& comp)
指定一個比較函數對象 comp 來建立set對象,內存分配器爲默認值。
//定義字符串比較函數對象 strLess
struct strLess {
bool operatro() (const char *s1, const char *s2) const
{
return strcmp(s1, s2) < 0;
}
};
//建立set容器對象s
set<const char*, strLess> s(strLess());
(3)set(const set&)
set拷貝構造函數,經過紅黑樹的拷貝構造函數,實現兩個set容器的元素、頭結點和節點個數的拷貝。
//set<int> s1;
set<int> s2 (s1);
(4)set(InputIterator first, InputIterator last)
用區間迭代器[first, last)所指的元素,建立一個set對象。
int iArray = { 13, 32,19 };
set<int> s(iArray, iArray+3);
(5)set(InputIterator first, InputIterator last, const key_compare& comp)
用區間迭代器[first, last)所指的元素和comp函數對象,建立一個set對象。
const char* szArray = {"hello", "dog", "bird" };
set<const char* , strLess> s(szArray, szArray+3, strLess() );
元素的插入
set沒有尾部插入函數push_back(),元素的插入通常使用insert進行動態檢索插入。
(1)pair<iterator, bool> insert(const value_type& v)
將元素v插入set容器,要求v值不與set容器的任何元素重複,不然插入失敗。返回一個pair配對對象,提供所插入元素的迭代器位置和true/false插入成功標誌。
(2)iterator insert(iterator position, const value_type& v)
將元素v插入set容器,參數position提示可在position位置以前插入v,所返回的插入位置視實際狀況而定,不必定能在position位置以前插入。
(3)void insert(inputIterator fist, InputIterator last)
將某迭代器區間[first, last)所指的數據做爲元素,插入到set容器。
元素的刪除
與插入同樣,set容器也具備高效的紅黑樹元素的刪除處理, 並自動從新調整內部的紅黑樹平衡。
(1) void erase(iterator position)
刪除position所指的元素
(2)size_type erase(const key_type& k)
刪除等於鍵值k的那個元素,對於set容器來講,此函數老是返回值1, 由於set容器不會出現重複的元素值(鍵值)。
(3)void erase(iterator first, iterator last)
刪除set迭代器區間[first, last)上的全部元素。
(4)void clear()
刪除全部元素,但不會刪除內部紅黑樹的頭節點。
元素的遍歷訪問
set容器的迭代器提供了訪問內部紅黑樹中元素的操做,一般先用begin和end函數找出遍歷開始的首元素和結束元素,而後經過迭代器的「++」操做,有小到到大取出元素值。
iterator begin();
iterator end();
元素的反向遍歷
利用set容器定義的方向迭代器reverse_iterator和const_reverse_iterator,可實現紅黑樹的逆中序遍歷,從而將元素從大到小遍歷出來。
reverse_iterator rbegin();
reverse_iterator rend();
元素的搜索
set容器提供了一個應用紅黑樹進行搜索的函數find,返回的迭代器值爲搜索到的元素位置,若是元素不存在,則返回end結束元素位置
iterator find(const key_type& k) constios
set的基本操做:less
begin() 返回指向第一個元素的迭代器函數
clear() 清除全部元素spa
count() 返回某個值元素的個數code
empty() 若是集合爲空,返回true對象
end() 返回指向最後一個元素的迭代器blog
equal_range() 返回集合中與給定值相等的上下限的兩個迭代器內存
erase() 刪除集合中的元素element
find() 返回一個指向被查找到元素的迭代器字符串
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大於(或等於)某值的第一個元素的迭代器
key_comp() 返回一個用於元素間值比較的函數
max_size() 返回集合能容納的元素的最大限值
rbegin() 返回指向集合中最後一個元素的反向迭代器
rend() 返回指向集合中第一個元素的反向迭代器
size() 集合中元素的數目
swap() 交換兩個集合變量
upper_bound() 返回大於某個值元素的迭代器
value_comp() 返回一個用於比較元素間的值的函數
5,自定義比較函數:
1 #include<iostream> 2 #include<set> 3 using namespace std; 4 typedef struct { 5 int a,b; 6 char s; 7 }newtype; 8 struct compare //there is no (). 9 { 10 bool operator()(const newtype &a, const newtype &b) const 11 { 12 return a.s<b.s; 13 } 14 };//the 「; 」 is here; 15 set<newtype,compare>element; 16 int main() 17 { 18 newtype a,b,c,d,t; 19 a.a=1; a.s='b'; 20 b.a=2; b.s='c'; 21 c.a=4; c.s='d'; 22 d.a=3; d.s='a'; 23 element.insert(a); 24 element.insert(b); 25 element.insert(c); 26 element.insert(d); 27 set<newtype,compare>::iterator it; 28 for(it=element.begin(); it!=element.end();it++) 29 cout<<(*it).a<<" "; 30 cout<<endl; 31 for(it=element.begin(); it!=element.end();it++) 32 cout<<(*it).s<<" "; 33 }
6.其餘的set構造方法:
1 #include <iostream> 2 #include <set> 3 using namespace std; 4 bool fncomp (int lhs, int rhs) {return lhs<rhs;} 5 struct classcomp { 6 bool operator() (const int& lhs, const int& rhs) const 7 {return lhs<rhs;} 8 }; 9 int main () 10 { 11 set<int> first; // empty set of ints 12 int myints[]= {10,20,30,40,50}; 13 set<int> second (myints,myints+5); // pointers used as iterators 14 set<int> third (second); // a copy of second 15 set<int> fourth (second.begin(), second.end()); // iterator ctor. 16 set<int,classcomp> fifth; // class as Compare 17 bool(*fn_pt)(int,int) = fncomp; 18 set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare 19 return 0; 20 }