Queue, Priority Queue and Vector in C++

Priority Queue Queue Vector
#include <queue> #include <queue> #include <vector>
priority_queue<int>
pq(nums.begin(), nums.end());
queue<int> q; vector<int> v;
hq.push(n); q.push(n); v.push_back(n);
int n = hq.top();
int n = q.front();
int n = v.front();
hq.pop(); q.pop(); v.pop_back();
  int n = q.back(); int n = v.back();

queue::pop() vs. queue::front()

The element removed is the “oldest” element in the queue whose value can be retrieved by calling member queue::front.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <queue>
using namespace std;

int main() {
    queue<int> q;
    q.push(3);
    q.push(5);
    q.push(7);
    q.push(4);

    int front1 = q.front(); // front1 = 3
    q.pop();
    int front2 = q.front(); // front1 = 5
    return 0;
}