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

C++ Usage and example of Queue pop() function

C++ STL Queue (Queue)

C ++ The Queue pop() function is used to delete the first element of the queue.

Syntax

void pop()

Parameters

This function only performs deletion operations and does not accept any parameters.

Return value

This function does not return a value. It is used to delete elements.

Instance1

#include <iostream>
#include <queue>
int main()
{
		std::queue<int> newqueue;
		int qint;
		std::cout << "Enter some valid integer values (enter 0 to end)";
		do
		{
			std::cin >> qint;
			newqueue.push(qint);
		}
		std::cout << "newqueue contains: ";
		while(!newqueue.empty())
		{
			std::cout << " " << newqueue.front();
			newqueue.pop();
		}
		return 0;
}

Output:

Enter some valid integer values (enter 0 to end)
1 
3
4
5
6
7
0
The newqueue contains: 1 3 4 5 6 7 0

Instance2

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	{
		int a = 0;
		queue<int> newqueue;
		newqueue.push(4);
		newqueue.push(8);
		newqueue.push(12);
		newqueue.push(16);
		while(!newqueue.empty())
		{
			newqueue.pop();
			a++;
		}
		cout << a;
	}
}

Output:

4

Complex

The complexity of the function is constant.

Data Races

This function modifies the container and all its elements. After an element is removed from the queue, the corresponding positions of all other elements will also be modified.

Exception Safety

It provides an equivalent guarantee for operations performed on underlying container objects.

C++ STL Queue (Queue)