STL Containers

容器的共性

1,所有容器中元素都是值而不是引用,元素插入容器的時(shí)候容器拷貝或移動(dòng)元素,即元素必須能夠被拷貝或移動(dòng)。但也可以存儲(chǔ)元素的指針來(lái)避免拷貝。

2,容器中的元素都是有特定順序的,不管是不是有序或無(wú)序容器,只要沒(méi)有進(jìn)行插入和刪除操作,利用迭代器遍歷多次得到的順序是一樣的。

3,STL本身一般不拋出異常

Req表示容器通用的操作

移動(dòng)迭代器

std::vector<std::string> c(std::make_move_iterator(l.begin()),std::make_move_iterator(l.end()));

獲取數(shù)組迭代器

int arr;

std::begin(arr),std::end(arr)

流迭代器

std::deque<int> c((std::istream_iterator<int>(std::cin)),(std::istream_iterator<int>()));

All containers except vectors and deques guarantee that iterators and references to elements remain valid if other elements are deleted. For vectors, only the elements before the point of erase remain valid.

If you remove all elements by using clear(), for vectors, deques, and strings any past-the-end iterator returned by end() or cend() may become invalid.

If you insert elements, only lists, forward lists, and associative containers guarantee that iterators and references to elements remain valid. For vectors, this guarantee is given if insertions don’t exceed the capacity. For unordered containers, that guarantee is given to references in general but to iterators only when no rehashing happens, which is guaranteed as long as with insertions the number of resulting elements is less than the bucket count times the maximum load factor.

容器通用類(lèi)型

Arrays

類(lèi)array<>定義在<array>,為普通的靜態(tài)數(shù)組提供容器的操作,未提供參數(shù)為初始值列表的構(gòu)造函數(shù)和賦值操作,但提供了移動(dòng)操作

std::array<int, 5> ?coll = { 42, 377, 611, 21, 44 };

array的元素是連續(xù)存儲(chǔ)的

std::array<char, 41>a; // create static array of 41 chars

strcpy(a.data(),"hello, world"); // copy a C-string into the array

printf("%s\n", a.data()); // print contents of the array as C-string

array提供tuple接口

typedef std::array<std::string, 5> ?FiveStrings;

FiveStrings a = { "hello", "nico", "how", "are", "you" };

std::tuple_size<FiveStrings>::value // yields 5

std::tuple_element<1,FiveStrings>::type // yields std::string

std::get<1>(a) // yields std::string("nico")

// negate all elements

transform(a.begin(),a.end(), a.begin(),negate<int>()); // operation

Vectors

reserve 和 shrink_to_fit使元素的引用,指針,迭代器無(wú)效

vec.data()返回?cái)?shù)據(jù)的開(kāi)始指針(STL實(shí)現(xiàn)vector是連續(xù)的存儲(chǔ)空間)

copy (sentence.cbegin(), sentence.cend(),ostream_iterator(cout," "));//打印

vector<bool> 中的元素一般為1bit

Deques

Lists

Forward Lists

不提供size()方法,可用distance算法計(jì)算

可唯一直接訪問(wèn)的元素是第一個(gè)

不提供后向迭代器,不可使用需要雙向迭代器的算法,比如sort(),但提供成員函數(shù)sort()替代

Sets and Multisets

一般使用平衡二叉樹(shù)(紅黑樹(shù),改變?cè)睾退阉髟氐男阅茌^好)

可以定義排序準(zhǔn)則作為模板參數(shù)或構(gòu)造參數(shù),否則使用默認(rèn)的排序準(zhǔn)則(<)

sets不允許重復(fù)元素,當(dāng)插入一個(gè)已存在元素時(shí)會(huì)失敗,同時(shí)返回此時(shí)的狀態(tài)

Maps and Multimaps

元素是鍵值對(duì),按照鍵的一定順序?qū)⒃嘏判颍琺ultimaps允許重復(fù)的鍵;<map>

注意:鍵和值必須支持拷貝或移動(dòng)操作,鍵必須支持某種排序規(guī)則;

內(nèi)部元素組織結(jié)構(gòu)類(lèi)似于set和multiset

傳遞排序規(guī)則可以使用模板參數(shù)的形式和構(gòu)造函數(shù)參數(shù)形式

map<string, int>::value_type類(lèi)型表示此map元素的類(lèi)型

使用value_type和pair<>及make_pair()構(gòu)造map的元素

at()函數(shù)返回元素的值得引用或者out_of_range異常如果沒(méi)有對(duì)應(yīng)的元素;[]操作符如果沒(méi)有此元素則會(huì)插入新的,可能會(huì)無(wú)意中插入新的值

Unordered Containers

<unordered_set>和<unordered_map>

實(shí)現(xiàn)是基于hash表,每個(gè)bucket里的元素用鏈表組織

元素組織結(jié)構(gòu)

具有常數(shù)時(shí)間的插入,刪除,搜索性能(但是發(fā)生rehash時(shí)是現(xiàn)行復(fù)雜度)

影響hash性能的操作,load_factor = 元素個(gè)數(shù)/桶的個(gè)數(shù),表征容器的滿(mǎn)的程度

實(shí)現(xiàn)引用語(yǔ)義的方法

1,shared_ptr<>,容器的元素是指針

2,std::reference_wapper<>,容器存儲(chǔ)的是元素的引用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容