In this instructional exercise you will find out about STL lien holder in C++ or std:: queue and all capacities which it gives.
std:: queue is a holder connector. Since the line is a compartment connector, this class utilizes a typified article.
It works in such manner that First in First out (FIFO) sort of game plan. Continuously components embedded at posterior and erased from the front side of the line.
C++ STL Queue Container Adaptor – std::queue
To work with the line we have to incorporate line header document
#include <queue>
The activities that can be applied online are:
void(): This is Boolean work. Returns whether the line is vacant or not.
push(element): This capacity embed component at the rear of the line.
pop (component): This capacity expels the component from the line. The component will be expelled from the front side.
back(): restores the pointer reference of the last component of the line.
front(): restores the pointer reference of the primary component of the line.
size(): restores the size of the line.
Model program to show the above capacities:
#include <iostream>
#include <queue>
using namespace std;
void displayQ( queue <int> q1){
for(int i=0; i<5; i++){
cout << q1.front() << " ";
q1.pop();
}
cout << endl;
}
int main(){
queue <int> q1;
for (int i=0; i<5; i++){
// inserting elements into the queue
q1.push(i+1);
}
// calling display function to display elements in the queue
cout << "the elements in the queue are ";
displayQ(q1);
cout << "the first element in queue is ";
cout << q1.front() << endl;
cout << "the last element in queue is ";
cout << q1.back() << endl;
cout << "size of the queue is " ;
cout << q1.size() << endl;
if (q1.empty()==1){
cout << "queue is empty" << endl;
}
else{
cout << "queue is not empty " << endl;
}
return 0;
}
Output
the elements in the queue are 1 2 3 4 5
the first element in queue is 1
the last element in queue is 5
size of the queue is 5
queue is not empty
Swap activity of lines:
This activity swaps the components of queue1 to queue2 and components of queue2 to queue1.
Model program to show swap activity:
#include <iostream>
#include <queue>
using namespace std;
void displayQ( queue <int> q1){
for(int i=0; i<5; i++){
cout << q1.front() << " ";
q1.pop();
}
cout << endl;
}
int main(){
queue <int> q1;
queue <int> q2;
for (int i=0; i<5; i++){
// inserting elements into the queue 1
q1.push(i+1);
}
cout << "elements 1,2,3,4,5 inserted into queue 1" << endl;
for (int i=0; i<5; i++){
// inserting elements into the queue 2
q2.push(i*10);
}
cout << "elements 0,10,20,30,40 inserted into queue 2" << endl;
q1.swap(q2);
cout << "contents after swapping operation" << endl;
cout << "elements of q1 are " << endl;
displayQ (q1);
cout << "elements of q2 are " << endl;
displayQ (q2);
return 0;
}
Output
elements 1,2,3,4,5 inserted into queue 1
elements 0,10,20,30,40 inserted into queue 2
contents after swapping operation
elements of q1 are
0 10 20 30 40
elements of q2 are
1 2 3 4 5
Remark beneath on the off chance that you have any questions identified with above C++ STL line compartment or std:: queue.