STL Containers

容器的共性

1,所有容器中元素都是值而不是引用,元素插入容器的時候容器拷貝或移動元素,即元素必須能夠被拷貝或移動。但也可以存儲元素的指針來避免拷貝。

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

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

Req表示容器通用的操作

移動迭代器

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.

容器通用類型

Arrays

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

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

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

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使元素的引用,指針,迭代器無效

vec.data()返回數(shù)據(jù)的開始指針(STL實現(xiàn)vector是連續(xù)的存儲空間)

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

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

Deques

Lists

Forward Lists

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

可唯一直接訪問的元素是第一個

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

Sets and Multisets

一般使用平衡二叉樹(紅黑樹,改變元素和搜索元素的性能較好)

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

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

Maps and Multimaps

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

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

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

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

map<string, int>::value_type類型表示此map元素的類型

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

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

Unordered Containers

<unordered_set>和<unordered_map>

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

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

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

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

實現(xiàn)引用語義的方法

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容