queue push() and pop() in C++ STL Last Updated : 26 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file.In this article we will learn how to use queue::push() and queue::pop() methods in C++.queue::push()The queue::push() function in C++ STL inserts an element at the back of the queue. This operation is called push operation hence the name push() method.Syntaxq.push(val);Parametersval: Value to insert.Return ValueThis function does not return any value.Example of queue::push() Method C++ // C++ Program to show how to use queue::push() // to push the element at the back of queue #include <bits/stdc++.h> using namespace std; int main() { queue<int> q; // Inserting elements at the back of queue q.push(0); q.push(1); q.push(2); // Printing queue while (!q.empty()) { cout << q.front() << " "; q.pop(); } return 0; } Output0 1 2 Time Complexity: O(1)Auxiliary Space: O(1)queue::pop() The queue::pop() function in C++ STL removes an element from the front of the queue. This operation is called pop operation hence the name. It works according to the FIFO (First-In-First-Out) order of deletion i.e. the element that was inserted first will be removed first.Syntaxq.pop();ParametersThis function does not take any parameter.Return ValueThis function does not return any value.Example of queue::pop() C++ // C++ Program to show how to use queue::pop() // to remove the element from the front of queue #include <bits/stdc++.h> using namespace std; int main() { queue<int> q; // Inserting few elements at the back of queue q.push(0); q.push(1); q.push(2); // Remove element from the front of queue q.pop(); // Printing queue while (!q.empty()) { cout << q.front() << " "; q.pop(); } return 0; } Output1 2 Time Complexity: O(1)Auxiliary Space: O(1)Difference Between queue::push() and queue::pop() The following table list the main differences between queue::push() and queue::pop():queue push() queue pop()It is used to push a new element at the end of the queue.It is used to remove the front element from the queue.Its syntax is -:q.push (val);Its syntax is -:q.pop();It takes one parameter that is the value to be inserted.It does not take any parameters. Comment More infoAdvertise with us Next Article queue::front() and queue::back() in C++ STL A AyushSaxena Follow Improve Article Tags : Misc C++ STL CPP-Library CPP-Functions cpp-containers-library cpp-queue +3 More Practice Tags : CPPMiscSTL Similar Reads Queue in C++ STL In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat 4 min read How to Iterate a STL Queue in C++? A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Syntax: queue<datatype> queuename;Datatype: Queue can take any data type depending on the values, e.g. int, char, float, etc. The std: :queue container d 4 min read queue push() and pop() in C++ STL The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file.In this arti 2 min read queue::front() and queue::back() in C++ STL Queue 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::front() This function is used to reference the first or the oldest element of the queue container. This function can 3 min read queue::empty() and queue::size() in C++ STL Queue is a type of container adaptor 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. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis 4 min read queue::emplace() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This fu 3 min read queue::swap() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() fun 2 min read Queue of Pairs in C++ STL with Examples Queue in STL are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement where elements are inserted at the back (end) and are deleted from the front. Queue of pair can be very efficient in designing complex data structures. The first element is referenced as âf 2 min read Queue using Stacks Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations.Table of ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making 11 min read Implement thread-safe queue in C++ What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr 3 min read Like