Sequence constainers(array and linked list)
–vector , deque, list , array
Associative Containers(binary tree)
–set, multiset
–map, multimap
Unordered Containers(hash table)
–unordered set/ multiset
–unordered map/multimap
vector
vector<int> vec(10,0) //创建容量为10的vector,并初始化为0
vector<int> vec = {1,4,8}
vector<int> vec2(vec) //copy constructor, vec2: {1,4,8}
vec.push_back(4);
vec.pop_back(); //移除最后一个元素
vec.size(); //vector元素数量大小
vec.clear(); //清空
//vec.front(); //返回第一个元素 == vec[0]
------------------------------------------------------------------------
vec.erase(vec.begin()); //删除迭代器所指向的向量元素
vec.insert(vec.begin(),100); //把100插入到向量中迭代器所指的位置
------------------------------------------------------------------------
for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
cout << *it <<endl;
sort(vec.begin(),vec.end());
for(int i : vec) cout << i << endl;
- fast insert/remove at the end: O(1)
- slow insert/remove at the begin or int the middle: O(n)
- slow search: O(n)
deque
deque<int> deq = {3,4,6};
deq.push_front(2);
deq.push_back(9);
- fast insert/remove at the begining and the end O(1);
- slow insert/remove in the middle: O(n)
- slow search: O(n)
list
list<int> li = {1,2,3};
li.push_back(6);
li.push_front(0);
- fast insert/remove at any place: O(1)
- slow serarch:O(n)
- no random access, no [] operator