The STL Stack (std::stack) is a Last In, First Out (LIFO) container adapter. It only supports querying or deleting the last inserted element (the top element) and does not support random access. To ensure strict data ordering, it does not support iterators.
Header File
1
#include<stack>
Definition
123
std::stack<TypeName>s;// Uses the default underlying container deque, data type is TypeNamestd::stack<TypeName,Container>s;// Uses Container as the underlying containerstd::stack<TypeName>s2(s1);// Constructs s2 by copying s1
Member Functions
All functions below have constant time complexity.
top() Accesses the top element of the stack (error if the stack is empty)
push(x) Inserts element x into the stack
pop() Removes the top element from the stack
size() Returns the number of elements in the container
The STL Queue (std::queue) is a First In, First Out (FIFO) container adapter. It only supports querying or deleting the first inserted element (the front element) and does not support random access. To ensure strict data ordering, it does not support iterators.
Header File
1
#include<queue>
Definition
1234
std::queue<TypeName>q;// Uses the default underlying container deque, data type is TypeNamestd::queue<TypeName,Container>q;// Uses Container as the underlying containerstd::queue<TypeName>q2(q1);// Constructs q2 by copying q1
Member Functions
All functions below have constant time complexity.
front() Accesses the front element of the queue (error if the queue is empty)
push(x) Inserts element x into the queue
pop() Removes the front element from the queue
size() Returns the number of elements in the container
The priority queue std::priority_queue is a type of heap, typically a binary heap.
Header File
1
#include<queue>
Definition
1 2 3 4 5 6 7 8 9101112131415161718
std::priority_queue<TypeName>q;// Data type is TypeNamestd::priority_queue<TypeName,Container>q;// Uses Container as the underlying containerstd::priority_queue<TypeName,Container,Compare>q;// Uses Container as the underlying container and Compare as the comparison type// By default, uses vector as the underlying container// Comparison type is less<TypeName> (in this case, top() returns the maximum value)// If you want top() to return the minimum value, set the comparison type to greater<TypeName>// Note: You cannot skip Container and directly pass Compare// Starting from C++11, if using a lambda function to customize Compare,// it must be passed as a constructor argument, for example:autocmp=[](conststd::pair<int,int>&l,conststd::pair<int,int>&r){returnl.second<r.second;};std::priority_queue<std::pair<int,int>,std::vector<std::pair<int,int>>,decltype(cmp)>pq(cmp);
Member Functions
All functions below have constant time complexity.
top() Accesses the top element of the heap (the priority queue must not be empty)
empty() Checks whether the container is empty
size() Returns the number of elements in the container
All functions below have logarithmic time complexity.
push(x) Inserts an element and sorts the underlying container
pop() Removes the top element of the heap (the priority queue must not be empty)
Simple Example
1 2 3 4 5 6 7 8 91011121314151617
std::priority_queue<int>q1;std::priority_queue<int,std::vector<int>>q2;// Spaces can be omitted after C++11std::priority_queue<int,std::deque<int>,std::greater<int>>q3;// q3 is a min-heapfor(inti=1;i<=5;i++)q1.push(i);// Elements in q1: [1, 2, 3, 4, 5]std::cout<<q1.top()<<std::endl;// Output: 5q1.pop();// Heap elements: [1, 2, 3, 4]std::cout<<q1.size()<<std::endl;// Output: 4for(inti=1;i<=5;i++)q3.push(i);// Elements in q3: [1, 2, 3, 4, 5]std::cout<<q3.top()<<std::endl;// Output: 1