Basic usage of vector, stack, queue in STL
In the past few days, I started to brush LeetCode. In addition to dividing knowledge points by myself, I also follow up on one question every day. I hope I can stick to it.
Personally, I feel that the vector, stack, and queue usages listed below are basically sufficient when brushing questions, which are more practical parts, and will be added later if they are not perfect.
For a detailed introduction to these containers, including how to construct and various assignment access operations, I will specialize in blog and code introduction in the near future.
1.vector
Vector is a vector type, which can store many types of data.
head File:#include<vector>
Related operation functions
Function name | content |
---|---|
empty() | Determine whether the vector is empty |
size() | Returns the number of elements in the vector |
push_back() | Add elements to the end of the vector |
emplace_back() | C++11 new feature, add elements at the end of the vector, construct the elements in place, without triggering copy construction and transfer construction |
pop_back() | Remove the tail elements of the vector |
front() | Returns the top element of the vector |
2.stack
The stack stack is a first-in-last-out FILO data structure.
head File:#include<stack>
Related operation functions
Function name | content |
---|---|
empty() | Determine whether the stack is empty |
size() | Returns the number of elements in the stack |
push() | Add elements to the top of the stack |
pop() | Remove top element |
top() | Return the top element of the stack |
3.queue
The queue is a first-in first-out FIFO data structure.
head File:#include<queue>
Related operation functions
Function name | content |
---|---|
empty() | Determine whether the queue is empty |
size() | Returns the number of elements in the queue |
push() | Add elements at the end of the line |
pop() | Remove the element at the head of the line |
front() | Return element |
The priority queue is actually a heap. The smallest/largest element can be taken out in O(1) time, and the element can be deleted in O(logn) time.
For example, the small top pile.
priority_queue<int, vector<int>, greater<int> > pq;
Function name | content |
---|---|
empty() | Determine whether the queue is empty |
size() | Returns the number of elements in the queue |
push() | Add elements at the end of the line |
pop() | Remove the element at the head of the line |
top() | Return element |
Welcome to reprint, indicate the source.