![]() |
VOOZH | about |
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++.
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.
q.push(val);
Parameters
Return Value
0 1 2
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
q.pop();
Parameters
Return Value
1 2
Time Complexity: O(1)
Auxiliary Space: O(1)
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. |