ref : https://srhuang.github.io/c++/2019/11/15/cplusplus-004.html
Name | Brief | Iterators | Capacity | Element Access | Modifiers | Others |
---|---|---|---|---|---|---|
array | Static contiguous array | begin, end | size, empty | operator[], at, front, back | swap | |
vector | Dynamic contiguous array | begin, end | size, empty, capacity | operator[], at, front, back | push_back, pop_back, insert, erase, swap, clear | |
deque | Double-ended queue | begin, end | size, empty | operator[], at, front, back | push_back, pop_back, insert, erase, swap, clear | |
list | Doubly-linked list | begin, end | size, empty | front, back | push_back, pop_back, insert, erase, swap, clear | sort, reverse |
forward_list | Singly-linked list | begin, end | empty | front | push_front, pop_front, insert_after, erase_after, swap, clear | sort, reverse |
針對x86_64系統。
char : 1 bytes
short : 2 bytes
int : 4 bytes
long : 8 bytes
Type | Size | Range |
---|---|---|
char | 1byte | -127 to 127 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
short int | 2bytes | -32,768 to 32,767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32,768 to 32,767 |
int | 4bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2,147,483,648 to 2,147,483,647 |
long | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
long int | 8bytes | same as long |
signed long int | 8bytes | same as long int |
unsigned long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | |
double | 8bytes | |
long double | 12bytes | |
wchar_t | 2 or 4 bytes | 1 wide character |
std::array這個container把fixed sized array做一層包裝。
因為使用priority_queue會有額外的空間,其實,vector就可以當成heap使用。這樣空間成本就會變成O(1)
預設set會從小到大排序。
set容器裡面的元素是唯一的,具有不重複的特性
multiset可以允許元素重複。
unordered_set 不排序的set。
unordered_multiset 不排序的multiset。
Map 的 key-value 對應主要用於資料一對一映射 (one-to-one) 的情況。
使用unordered_map的時候,根據key產生出來的hash來查找value。既然是hash就會有碰撞問題。根據unordered_map reserve() in C++ STL敘述。
As we know a Bucket is a slot in the container’s internal hash table to which all the element are assigned based on the hash value of their key . Buckets are numbered from 0 to bucket_count.
如果增加的數目超出bucket_count,map就會自動變大bucket_slot,並且重新計算所有item的hash。
When the Load Factor(load_factor) reaches a certain threshold, the container increases the number of buckets and rehashes the map.
使用以下的程式碼,就是放大bucket到n,並且重新計算hash table。
如果我們事先知道大小可以使用以下function直接保留bucket到n,避免超出threshold需要放大container。因為事先保留了n個bucket也可以避免hash collision。
也可以找非1D vector。例如
string 是一個保存 char 的序列容器,把字串的記憶體管理責任交由 string 負責而不是 programmer,減輕了 C 語言風格字串的麻煩。
istringstream : input string stream。
ostringstream : output string stream。
stringstream : 可以使用在input/output。
ref : Processing strings using std::istringstream
可以用iss來判斷最後取值是否成功
如果資料是混合型態
箭頭表示繼承,所以Random Access Iterator可以是bidirectional,也可以是Forward Iterator或是Input Iterator。
Type of Iterator | Container |
---|---|
Random Access | vector, deque |
bidirectional | list, map, multimap, set, multiset |
Type of Iterator | Limitations |
---|---|
bidirectional | 1.Relational Operators(<=, >=) 2.Arithmetic Operators(+=, -=, +, -) 3.Use of offset dereference operator ([ ]) |
再c++中有STL algorithm裡面有很多好用的function。
c++中沒內建計算排列組合的函式,但是簡單的排列組合,可用以下來計算。