Queue Push Function - C++ Programming Tutorial
C++ Course / Data Structures / Queue Push Function

Queue Push Function

BLUF: Mastering Queue Push Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Queue Push Function

C++ is renowned for its efficiency. Learn how Queue Push Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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

Example

#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:

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

Example

#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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience