Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be of same type, although sizes may differ.
queue::emplace() in C++ STL: Insert a new element into the queue container, the new element is added to the end of the queue.
queue::front() and queue::back() in C++ STL– front() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.
push(g) and pop() – push() function adds the element ‘g’ at the end of the queue. pop() function deletes the first element of the queue.
Queues Example
#include<iostream>#include<queue>usingnamespace std;intmain() { cout <<"\nC++ Queue Code"<< endl; /* Queues are First-In/First-Out. This is like a line to pay when shopping. The first person to enter the line is the first person to get to a register to pay for their items. */
// Create a queue that holds integers. queue<int> q; // Add some numbers to the stack.q.push(4);q.push(2);q.push(1);q.push(3); // The stack should be 4 > 2 > 1 > 3, with 4 in front, since that's the order we pushed them in. // Print each number and remove it from the queue. cout <<"Printed queue: "<< endl;while(q.empty() ==false) {int frontnum =q.front(); // Look at what the front of the queue is. cout << frontnum <<" ";q.pop(); // Remove the top item from the stack. } cout << endl; // Let's look at a queue of nodes. /* NOTICE: This is the same as stacks, except the default queue creation (queue<Food*> foodstack;) is the only difference. */
structFood { // The three variables we care about.bool tastesGood;int quantity; string name; // Constructor for this node.Food(bool taste,int quant,string n) { tastesGood = taste; quantity = quant; name = n; } // Default constructor if no info is given.Food():tastesGood(false),quantity(0),name("Air") {} }; /* Node initialization is as such Food* myFood = new Food(true, 100, "chicken"); cout << myFood->name << endl; */ // Create our queue of foods. queue<Food*> foodq; // Add some nodes to this queue.foodq.push(newFood(false,1,"Apple"));foodq.push(newFood(true,2,"Banana"));foodq.push(newFood(true,5,"Eclair"));foodq.push(newFood(true,3,"Chocolate"));foodq.push(newFood(true,4,"French Fries")); // Print each food in our queue. (It'll go in the order we placed them in the queue, regardless of content.) cout << endl <<"Printed Food queue: "<< endl;int counter =0;while(foodq.empty() ==false) { counter +=1; Food* food =foodq.front();foodq.pop(); cout <<"Food number "<< counter <<" has "<<food->quantity <<" of "<<food->name <<" and it ";if(food->tastesGood ==true) { cout <<"tastes good!"<< endl; }else { cout <<"tastes bad!"<< endl; } }return0; }// Output: 4213Printed Food queue: Food number 1 has 1 of Apple and it tastes bad!Food number 2 has 2 of Banana and it tastes good!Food number 3 has 5 of Eclair and it tastes good!Food number 4 has 3 of Chocolate and it tastes good!Food number 5 has 4 of French Fries and it tastes good!