c++之stl 二叉樹

來源:互聯網
上載者:User

   set是根據元素值進行排序的集合,所插入的元素在集合中唯一,不存在重複元素。

set由二叉搜尋樹實現,並且對樹進行了平衡處理,使得元素在樹中分部較為均勻,因此能保持搜尋、插入、刪除的複雜度在O(logn)。

函數名 功能 複雜度
size() 返回set中的元素數 O(1)
clear() 清空set O(n)
begin() 返回指向set開頭的迭代器 O(1)
end() 返回指向set末尾的迭代器 O(1)
insert(key) 向set中插入元素key O(logn)
erase(key) 刪除含有key的元素 O(logn)
find(key) 搜尋與key一致的元素,並返回指向該元素的迭代器。
沒有與key一致的元素,則返回末尾end()
O(logn)
     map集合以鍵與值的組合為元素,每個元素擁有一個鍵和一個值,集合以鍵作為排序標準。集合中各個元素的鍵唯一,不存在重複。map可以看作是一種能使用任意類型下標的關聯式容器。比如我們可以用它來實現“從字串中刪除字串”這類字典功能。

執行個體代碼:

#include<iostream>#include<map>#include<string>using namespace std;void print(map<string,int> T){map<string,int>::iterator it;cout<<T.size()<<endl;for(it=T.begin();it!=T.end();it++){pair<string,int> item=*it;cout<<item.first<<"-->"<<item.second<<endl;}}int main(){map<string,int> T;T["red"]=32;T["blue"]=688;T["yellow"]=122;T["blue"]+=312;print(T);T.insert(make_pair("zebra", 101010));T.insert(make_pair("white", 0));print(T);pair<string, int> target = *T.find("red");cout<<target.first<<"-->"<<target.second<<endl;return 0;} 

運行結果:


函數名 功能 複雜度
size() 返回map中的元素數 O(1)
clear() 清空map O(n)
begin() 返回指向map開頭的迭代器 O(1)
end() 返回指向map末尾的迭代器 O(1)
insert(key,val) 向map中插入元素(key,val) O(logn)
erase(key) 刪除含有key的元素 O(logn)
find(key) 搜尋與key一致的元素,並返回指向該元素的迭代器。
沒有與key一致的元素,則返回末尾的end()
map實際成員是個pair結構體,所以返回的迭代器是指向pair的。
O(logn)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.