1.關於set
C++ STL 之所以得到廣泛的讚譽,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封裝了許多複雜的數據結構算法和大量常用數據結構操作。vector封裝數組,list封裝了鏈表,map和set封裝了二叉樹等,在封裝這些數據結構的時候,STL按照程序員的使用習慣,以成員函數方式提供的常用操作,如:插入、排序、刪除、查找等。讓用戶在STL使用過程中,並不會感到陌生。
關於set,必須說明的是set關聯式容器。set作爲一個容器也是用來存儲同一數據類型的數據類型,並且能從一個數據集合中取出數據,在set中每個元素的值都唯一,而且系統能根據元素的值自動進行排序。應該注意的是set中數元素的值不能直接被改變。C++ STL中標準關聯容器set, multiset, map, multimap內部採用的就是一種非常高效的平衡檢索二叉樹:紅黑樹,也成爲RB樹(Red-Black Tree)。RB樹的統計性能要好於一般平衡二叉樹,所以被STL選擇作爲了關聯容器的內部結構。
關於set有下面幾個問題:
(1)爲何map和set的插入刪除效率比用其他序列容器高?
大部分人說,很簡單,因爲對於關聯容器來說,不需要做內存拷貝和內存移動。說對了,確實如此。set容器內所有元素都是以節點的方式來存儲,其節點結構和鏈表差不多,指向父節點和子節點。結構圖可能如下:
A
/
B C
/ \ /
D E F G
因此插入的時候只需要稍做變換,把節點的指針指向新的節點就可以了。刪除的時候類似,稍做變換後把指向刪除節點的指針指向其他節點也OK了。這裏的一切操作就是指針換來換去,和內存移動沒有關係。
(2)爲何每次insert之後,以前保存的iterator不會失效?
iterator這裏就相當於指向節點的指針,內存沒有變,指向內存的指針怎麼會失效呢(當然被刪除的那個元素本身已經失效了)。相對於vector來說,每一次刪除和插入,指針都有可能失效,調用push_back在尾部插入也是如此。因爲爲了保證內部數據的連續存放,iterator指向的那塊內存在刪除和插入過程中可能已經被其他內存覆蓋或者內存已經被釋放了。即使時push_back的時候,容器內部空間可能不夠,需要一塊新的更大的內存,只有把以前的內存釋放,申請新的更大的內存,複製已有的數據元素到新的內存,最後把需要插入的元素放到最後,那麼以前的內存指針自然就不可用了。特別時在和find等算法在一起使用的時候,牢記這個原則:不要使用過期的iterator。
(3)當數據元素增多時,set的插入和搜索速度變化如何?
如果你知道log2的關係你應該就徹底瞭解這個答案。在set中查找是使用二分查找,也就是說,如果有16個元素,最多需要比較4次就能找到結果,有32個元素,最多比較5次。那麼有10000個呢?最多比較的次數爲log10000,最多爲14次,如果是20000個元素呢?最多不過15次。看見了吧,當數據量增大一倍的時候,搜索次數只不過多了1次,多了1/14的搜索時間而已。你明白這個道理後,就可以安心往裏面放入元素了。
2.set中常用的方法
begin() 返回set容器的第一個元素
end() 返回set容器的最後一個元素(end()返回的是最後一個元素的下一個元素的索引,不是最後一個元素)
clear() 刪除set容器中的所有的元素
empty() 判斷set容器是否爲空
max_size() 返回set容器可能包含的元素最大個數
size() 返回當前set容器中的元素個數
rbegin 返回的值和end()相同
rend() 返回的值和rbegin()相同
寫一個程序練一練這幾個簡單操作吧:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> s;
9 s.insert(1);
10 s.insert(2);
11 s.insert(3);
12 s.insert(1);
13 cout<<"set 的 size 值爲 :"<<s.size()<<endl;
14 cout<<"set 的 maxsize的值爲 :"<<s.max_size()<<endl;
15 cout<<"set 中的第一個元素是 :"<<*s.begin()<<endl;
16 cout<<"set 中的最後一個元素是:"<<*s.end()<<endl;
17 s.clear();
18 if(s.empty())
19 {
20 cout<<"set 爲空 !!!"<<endl;
21 }
22 cout<<"set 的 size 值爲 :"<<s.size()<<endl;
23 cout<<"set 的 maxsize的值爲 :"<<s.max_size()<<endl;
24 return 0;
25 }
運行結果:
小結:
插入3之後雖然插入了一個1,但是我們發現set中最後一個值仍然是3哈,這就是set 。還要注意begin() 和 end()函數是不檢查set是否爲空的,使用前最好使用empty()檢驗一下set是否爲空.
count() 用來查找set中某個某個鍵值出現的次數。這個函數在set並不是很實用,因爲一個鍵值在set只可能出現0或1次,這樣就變成了判斷某一鍵值是否在set出現過了。
示例代碼:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> s;
9 s.insert(1);
10 s.insert(2);
11 s.insert(3);
12 s.insert(1);
13 cout<<"set 中 1 出現的次數是 :"<<s.count(1)<<endl;
14 cout<<"set 中 4 出現的次數是 :"<<s.count(4)<<endl;
15 return 0;
16 }
運行結果:
set 中 1 出現的次數是 :1
set 中 4 出現的次數是 :0
equal_range() ,返回一對定位器,分別表示第一個大於或等於給定關鍵值的元素和 第一個大於給定關鍵值的元素,這個返回值是一個pair類型,如果這一對定位器中哪個返回失敗,就會等於end()的值。具體這個有什麼用途我還沒遇到過~~~
示例代碼:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> s;
9 set<int>::iterator iter;
10 for(int i = 1 ; i <= 5; ++i)
11 {
12 s.insert(i);
13 }
14 for(iter = s.begin() ; iter != s.end() ; ++iter)
15 {
16 cout<<*iter<<" ";
17 }
18 cout<<endl;
19 pair<set<int>::const_iterator,set<int>::const_iterator> pr;
20 pr = s.equal_range(3);
21 cout<<"第一個大於等於 3 的數是 :"<<*pr.first<<endl;
22 cout<<"第一個大於 3的數是 : "<<*pr.second<<endl;
23 return 0;
24 }
運行結果:
1 2 3 4 5
第一個大於等於 3 的數是 :3
第一個大於 3的數是 : 4
erase(iterator) ,刪除定位器iterator指向的值
erase(first,second),刪除定位器first和second之間的值
erase(key_value),刪除鍵值key_value的值
看看程序吧:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> s;
9 set<int>::const_iterator iter;
10 set<int>::iterator first;
11 set<int>::iterator second;
12 for(int i = 1 ; i <= 10 ; ++i)
13 {
14 s.insert(i);
15 }
16 //第一種刪除
17 s.erase(s.begin());
18 //第二種刪除
19 first = s.begin();
20 second = s.begin();
21 second++;
22 second++;
23 s.erase(first,second);
24 //第三種刪除
25 s.erase(8);
26 cout<<"刪除後 set 中元素是 :";
27 for(iter = s.begin() ; iter != s.end() ; ++iter)
28 {
29 cout<<*iter<<" ";
30 }
31 cout<<endl;
32 return 0;
33 }
運行結果:
刪除後 set 中元素是 :4 5 6 7 9 10
小結
:set中的刪除操作是不進行任何的錯誤檢查的,比如定位器的是否合法等等,所以用的時候自己一定要注意。
find() ,返回給定值值得定位器,如果沒找到則返回end()。
#include <iostream>
#include <set>
using namespace std;
int main()
{
int a[] = {1,2,3};
set<int> s(a,a+3);
set<int>::iterator iter;
if((iter = s.find(2)) != s.end())
{
cout<<*iter<<endl;
}
return 0;
}
運行結果:
2
insert(key_value); 將key_value插入到set中 ,返回值是pair<set::iterator,bool>,bool標誌着插入是否成功,而iterator代表插入的位置,若key_value已經在set中,則iterator表示的key_value在set中的位置。
inset(first,second);將定位器first到second之間的元素插入到set中,返回值是void.
示例代碼:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 int a[] = {1,2,3};
9 set<int> s;
10 set<int>::iterator iter;
11 s.insert(a,a+3);
12 for(iter = s.begin() ; iter != s.end() ; ++iter)
13 {
14 cout<<*iter<<" ";
15 }
16 cout<<endl;
17 pair<set<int>::iterator,bool> pr;
18 pr = s.insert(5);
19 if(pr.second)
20 {
21 cout<<*pr.first<<endl;
22 }
23 return 0;
24 }
運行結果:
1 2 3
5
lower_bound(key_value) ,返回第一個大於等於key_value的定位器
upper_bound(key_value),返回最後一個大於等於key_value的定位器
示例代碼:
1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> s;
9 s.insert(1);
10 s.insert(3);
11 s.insert(4);
12 cout<<*s.lower_bound(2)<<endl;
13 cout<<*s.lower_bound(3)<<endl;
14 cout<<*s.upper_bound(3)<<endl;
15 return 0;
16 }
運行結果:
3
3
4
三.自定義比較函數
(1)元素不是結構體:
例:
//自定義比較函數myComp,重載“()”操作符
struct myComp
{
bool operator()(const your_type &a,const your_type &b)
[
return a.data-b.data>0;
}
}
set<int,myComp>s;
......
set<int,myComp>::iterator it;
(2)如果元素是結構體,可以直接將比較函數寫在結構體內。
例:
struct Info
{
string name;
float score;
//重載“<”操作符,自定義排序規則
bool operator < (const Info &a) const
{
//按score從大到小排列
return a.score<score;
}
}
set<Info> s;
......
set<Info>::iterator it;