English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Queue push() function usage and example

C++ STL Queue (Queue)

C++ The queue push() function is used to add a new element to the end of the queue. This function is used to perform insertion-related operations.

Syntax

void push(const value_type& value);

Parameter

value:This parameter indicates the value initialized for the element. It is the value of the new element to be added to the queue.

Return value

This function does not return a type, it only adds a new element to the queue.

Instance1

#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

Instance2

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

34 68

Complex

A single call to the push operation on the underlying container will be made.

Data Races

The container and its elements have been modified.

Exception Safety

Guarantees operations equivalent to those performed on the underlying container objects.

C++ STL Queue (Queue)