queue in c++ code example

Example 1: stl queue

Functions used here:
   q.size() = Returns the size of queue.
   q.push() = It is used to insert elements to the queue.
   q.pop() = To pop out the value from the queue.
   q.front() = Returns the front element of the array.
   q.back() = Returns the back element of the array.

Example 2: priority queue in c++

//Shubh'grepper
// Implementation of priority_queue in c++

//queue with elements in decreasing order
priority_queue<int> pq;

// queue with elements in increasing order  using compare function inside declaration
priority_queue <int, vector<int>, greater<int> > pq;

//priority_queue of type pair<int, int>
#define pp pair<int, int>
priority_queue <pp, vector<pp>, greater<pp> > pq;

Example 3: queue c++

//Queue is a data structure designed to operate in FIFO(First in First out) context. 
//In queue elements are inserted from rear end and get removed from front end.
 The functions supported by queue:
 ---------------------------------
 empty()   | Tests whether queue is empty or not.
 size()    | Returns the total number of elements present in the queue.
 push()    | Inserts new element at the end of queue.
 emplace() | Constructs and inserts new element at the end of queue.
 pop()     | Removes front element of the queue.
 swap()    | Exchanges the contents of queue with contents of another queue.
 front()   | Returns a reference to the first element of the queue.
 back()    | Returns a reference to the last element of queue.

Tags:

Cpp Example