containers C++ 【updating】
我寫的這個系列強調如何通過C++來使用這些container,適合對資料結構有基本認識正在刷題的人使用。
Iterators
begin, end, rbegin, rend, cbegin, cend, crbegin, crend
begin - returns the beginning iterator
end - returns the ending iterator
r - in the reverse order
c - returns a const iterator
1. forward_list沒有reverse order因為它是單向的,但是有多餘的before_begin
2. unordered associative containers (unordered_set, unordered_map, unordered_multiset, unordered_multimap)沒有reverse因為它是unordered所以單向遍歷已經足夠了。
4. container adaptors (stack, queue, priority_queue)沒有iterators所以沒有辦法在不影響容器內元素的情況下遍歷。
Capacity
size, empty, max_size
size - returns the size
empty - check if the size is zero
max_size - the max size the container can hold
1. forward_list doesn't have size becuase it saves space (stackoverflow)
2. container adaptors(stack, queue, priority_queue) don't have max_size.
3. vector & deque have resize
4. vector has capacity and reserve. (size vs max_size vs capacity)
Element Access
front, back: sequence container (array, vector, deque, forward_list, list) and queue
* forward_list沒有back因為它是單向的
top: stack and priority_queue
[], at: array, vector, deque, map and unordered_map
* the others we use iterators through find
data: array, vector (contiguous)
Modifier
swap: all
insert, erase, clear: except for container adaptors (stack, queue, priority_queue)
*forward_list use insert_after and erase_after
assign: sequence container without array (vector, deque, forward_list, list)
push_back, pop_back: vector, deque, list
push_front, pop_front: forward_list, deque, list
push, pop: container adaptors (stack, queue, priority_queue)
Operations
splice, remove, remove_if, unique, merge, sort, reverse: forward_list and list
* forward_list use splice_after
find, count, equal_range: all except for sequence containers
* sequence containers use find from algorithm
lower_bound, upper_bound: associative containers (set, map, multiset, multimap)
Sequence Containers: array, vector, deque, forward_list, list
Associative Containers: set, map, multiset, multimap
Unordered Associative Containers: unordered_set, unordered_map, unordered_multiset, unordered_multimap
Container Adaptors: stack, queue, priority_queue
Special Attention to containers with order: set, map, multiset, multimap, priority_queue
How to write the comparator for them and the sort <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct Node{
int val;
Node* next;
Node(int x): val(x), next(NULL) {};
};
struct cmp{
bool operator() (Node* a, Node* b){
return a->val > b->val;
}
};
int main()
{
// ordered container
priority_queue<Node*> q1;
for(int i=0; i<10; i++){
q1.push(new Node(i));
}
cout<<"q1:";
while(!q1.empty()){
int t = q1.top()->val; q1.pop();
cout<<" "<<t;
}
cout<<endl;
// use the comparator to place the smallest to the top
priority_queue<Node*, vector<Node*>, cmp> q2;
for(int i=0;i<10; i++){
q2.push(new Node(i));
}
cout<<"q2:";
while(!q2.empty()){
int t = q2.top()->val; q2.pop();
cout<<" "<<t;
}
cout<<endl;
// unordered container
vector<Node*> vec{new Node(1), new Node(4), new Node(3), new Node(6)};
sort(vec.begin(), vec.end(), [](Node* a, Node* b){
return a->val < b->val;
});
cout<<"vec:";
for(Node* a: vec) cout<<" "<<a->val;
cout<<endl;
return 0;
}
The excel file records all the differences.