Queue in C++ Standard Template Library (STL)

06 Jul, 2022
Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. Queues use an encapsulated object of deque or list (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.
Following is an example to demonstrate the queue and its various methods.
CPP
// CPP code to illustrate Queue in // Standard Template Library (STL)#include <iostream>#include <queue>  using namespace std;  // Print the queuevoid showq(queue<int> gq){    queue<int> g = gq;    while (!g.empty()) {        cout << '\t' << g.front();        g.pop();    }    cout << '\n';}  // Driver Codeint main(){    queue<int> gquiz;    gquiz.push(10);    gquiz.push(20);    gquiz.push(30);      cout << "The queue gquiz is : ";    showq(gquiz);      cout << "\ngquiz.size() : " << gquiz.size();    cout << "\ngquiz.front() : " << gquiz.front();    cout << "\ngquiz.back() : " << gquiz.back();      cout << "\ngquiz.pop() : ";    gquiz.pop();    showq(gquiz);      return 0;}
The queue gquiz is :     10    20    30

gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() :     20    30
Methods of Queue are: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
The time complexity and definition of the following functions are as follows:
C++
// CPP code to illustrate Queue operations in  STL// Divyansh Mishra  --> divyanshmishra101010#include <iostream>#include <queue>  using namespace std;  // Print the queuevoid print_queue(queue<int> q){    queue<int> temp = q;    while (!temp.empty()) {        cout << temp.front()<<" ";        temp.pop();    }    cout << '\n';}  // Driver Codeint main(){    queue<int> q1;    q1.push(1);    q1.push(2);    q1.push(3);      cout << "The first queue is : ";    print_queue(q1);         queue<int> q2;    q2.push(4);    q2.push(5);    q2.push(6);      cout << "The second queue is : ";    print_queue(q2);              q1.swap(q2);              cout << "After swapping, the first queue is : ";    print_queue(q1);      cout << "After swapping the second queue is : ";    print_queue(q2);          cout<<q1.empty();  //returns false since q1 is not empty      return 0;}
The first queue is : 1 2 3 
The second queue is : 4 5 6 
After swapping, the first queue is : 4 5 6 
After swapping the second queue is : 1 2 3 
0
C++ Programming Language Tutorial | Stacks and Queues using STL | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersC++ Programming Language Tutorial | Stacks and Queues using STL | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:39•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=fV_DchJem0U" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Recent Articles on C++ Queue   Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rohit kumar 37
rkmvcommon92569153
siddhartha_agarwal
lekhrajsharmaas
naveenkumar173
vijayt
anshikajain26
karunasharma12dec
divyanshmishra101010
cpp-containers-library
cpp-queue
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Arrays in C/C++
Set in C++ Standard Template Library (STL)
Inheritance in C++
unordered_map in C++ STL
vector erase() and clear() in C++