1. 程式人生 > >C++中set用法詳解

C++中set用法詳解

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容器的最後一個元素

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 }
複製程式碼

執行結果:

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 }
複製程式碼

執行結果:

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中的刪除操作是不進行任何的錯誤檢查的,比如定位器的是否合法等等,所以用的時候自己一定要注意。

find()  ,返回給定值值得定位器,如果沒找到則返回end()。

示例程式碼:

複製程式碼
 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(a,a+3);
10     set<int>::iterator iter;
11     if((iter = s.find(2)) != s.end())
12     {
13         cout<<*iter<<endl;
14     }
15     return 0;
16 }
複製程式碼

insert(key_value); 將key_value插入到set中 ,返回值是pair<set<int>::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 }
複製程式碼

執行結果:

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;