容器的共性
1空郊,所有容器中元素都是值而不是引用,元素插入容器的時(shí)候容器拷貝或移動(dòng)元素切揭,即元素必須能夠被拷貝或移動(dòng)狞甚。但也可以存儲(chǔ)元素的指針來避免拷貝。
2廓旬,容器中的元素都是有特定順序的哼审,不管是不是有序或無序容器,只要沒有進(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.
容器通用類型
Arrays
類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使元素的引用址儒,指針,迭代器無效
vec.data()返回?cái)?shù)據(jù)的開始指針(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ì)算
可唯一直接訪問的元素是第一個(gè)
不提供后向迭代器离福,不可使用需要雙向迭代器的算法,比如sort()炼蛤,但提供成員函數(shù)sort()替代
Sets and Multisets
一般使用平衡二叉樹(紅黑樹,改變元素和搜索元素的性能較好)
可以定義排序準(zhǔn)則作為模板參數(shù)或構(gòu)造參數(shù)蝶涩,否則使用默認(rèn)的排序準(zhǔn)則(<)
sets不允許重復(fù)元素理朋,當(dāng)插入一個(gè)已存在元素時(shí)會(huì)失敗,同時(shí)返回此時(shí)的狀態(tài)
Maps and Multimaps
元素是鍵值對绿聘,按照鍵的一定順序?qū)⒃嘏判蛩陨希琺ultimaps允許重復(fù)的鍵;<map>
注意:鍵和值必須支持拷貝或移動(dòng)操作熄攘,鍵必須支持某種排序規(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)的元素;[]操作符如果沒有此元素則會(huì)插入新的,可能會(huì)無意中插入新的值
Unordered Containers
<unordered_set>和<unordered_map>
實(shí)現(xiàn)是基于hash表浅萧,每個(gè)bucket里的元素用鏈表組織
具有常數(shù)時(shí)間的插入逐沙,刪除,搜索性能(但是發(fā)生rehash時(shí)是現(xiàn)行復(fù)雜度)
影響hash性能的操作洼畅,load_factor = 元素個(gè)數(shù)/桶的個(gè)數(shù)吩案,表征容器的滿的程度
實(shí)現(xiàn)引用語義的方法
1,shared_ptr<>帝簇,容器的元素是指針
2徘郭,std::reference_wapper<>,容器存儲(chǔ)的是元素的引用