The C++ Queue push method is employed to append new elements at the end of the queue. This function is intended for executing operations related to inserting elements.
Syntax
void push (const value_type& value);
Parameters
The value parameter signifies the initial value with which the element is set. It denotes the value of the newly appended element in the queue.
Return value
The function does not specify a return type and solely appends a fresh element to the queue.
Example 1
#include <iostream>
#include <queue>
int main()
{
std::queue<int> newqueue;
int qint;
std::cout << "Enter some valid integer values(press 0 to exit)";
do
{
std::cin>> qint;
newqueue.push(qint);
}
while (qint);
std::cout<< "newqueue contains: ";
while(!newqueue.empty())
{
std::cout <<" " <<newqueue.front();
newqueue.pop();
}
return 0;
}
Output:
Enter some valid integer values(press 0 to exit)
1
2
3
5
6
7
0
newqueue contains: 1 2 3 5 6 7 0
Example 2
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> newqueue;
newqueue.push(34);
newqueue.push(68);
while(!newqueue.empty())
{
cout<<" "<<newqueue.front();
newqueue.pop();
}
}
Output:
Complexity
A single invocation is made to the pushback function on the underlying container.
Data races
The adjustment is applied to the container as well as the enclosed elements.
Exception Safety
A promise is given that matches the actions carried out on the container object underneath.