Queue in Standard Template Library (STL)

Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. 队列是一种容器适配器,先入先出,元素插在队尾,在队头删除。

The functions supported by queue are :

  1. empty() – Returns whether the queue is empty.
  2. size() – Returns the size of the queue.
  3. queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be of same type, although sizes may differ.
  4. queue::emplace() in C++ STL: Insert a new element into the queue container, the new element is added to the end of the queue.
  5. queue::front() and queue::back() in C++ STL– front() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.
  6. push(g) and pop() – push() function adds the element ‘g’ at the end of the queue. pop()function deletes the first element of the queue.
// CPP code to illustrate 
// Queue in Standard Template Library (STL) 
#include <iostream> 
#include <queue> 

using namespace std; 

void showq(queue <int> gq) 
{ 
	queue <int> g = gq; 
	while (!g.empty()) 
	{ 
		cout << '\t' << g.front(); 
		g.pop(); 
	} 
	cout << '\n'; 
} 

int main() 
{ 
	queue <int> gquiz; 
	gquiz.push(10); 
	gquiz.push(20); 
	gquiz.push(30); 

	cout << "The queue gquiz is : "; 
	showq(gquiz); 

	cout << "\ngquiz.size() : " << gquiz.size(); 
	cout << "\ngquiz.front() : " << gquiz.front(); 
	cout << "\ngquiz.back() : " << gquiz.back(); 

	cout << "\ngquiz.pop() : "; 
	gquiz.pop(); 
	showq(gquiz); 

	return 0; 
} 

Output:

The queue gquiz is :     10    20    30

gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() :     20    30

queue::front() and queue::back() in C++ STL


Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.

queue::front()

This function is used to reference the first or the oldest element of the queue container. This function can be used to fetch the first element of a queue.函数front指向队列最早或最先插入的元素,获得这个元素。

Syntax :

queuename.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the queue container.

Examples:

Input  :  myqueue = 1, 2, 3
          myqueue.front();
Output :  1

Input  :  myqueue = 3, 4, 1, 7, 3
          myqueue.front();
Output :  3

Errors and Exceptions

  1. If the queue container is empty, it causes undefined behaviour
  2. It has a no exception throw guarantee if the queue is not empty
// CPP program to illustrate 
// Implementation of front() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<int> myqueue; 
	myqueue.push(3); 
	myqueue.push(4); 
	myqueue.push(1); 
	myqueue.push(7); 
	
	// Queue becomes 3, 4, 1, 7 

	cout << myqueue.front(); 
	return 0; 
} 

Output:

3

queue::back()

This function is used to reference the last or the newest element of the queue container. This function can be used to fetch the first element from the back of a queue. 获得最后插入的元素。

Syntax :

queuename.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the queue container.

Examples:

Input  :  myqueue = 1, 2, 3
          myqueue.back();
Output :  3

Input  :  myqueue = 3, 4, 1, 7, 3
          myqueue.back();
Output :  3

Errors and Exceptions

  1. If the queue container is empty, it causes undefined behaviour
  2. It has a no exception throw guarantee if the queue is not empty
// CPP program to illustrate 
// Implementation of back() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<int> myqueue; 
	myqueue.push(3); 
	myqueue.push(4); 
	myqueue.push(1); 
	myqueue.push(7); 

	// Queue becomes 3, 4, 1, 7 

	cout << myqueue.back(); 
	return 0; 
} 

Output:

7

Application :
Given an empty queue of integers, add numbers to the queue, then print the difference between the first and the last element.

Input  : 1, 2, 3, 4, 5, 6, 7, 8
Output : 7
Explanation - Last element = 8, First element = 1, Difference = 7

Algorithm
1. Add numbers to the queue using push() function
2. Compare the first and the last element.
3. If first element is larger, subtract last element from it and print it.
4. Else subtract first element from the last element and print it.

// CPP program to illustrate 
// application Of front() and back() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<int> myqueue; 
	myqueue.push(8); 
	myqueue.push(7); 
	myqueue.push(6); 
	myqueue.push(5); 
	myqueue.push(4); 
	myqueue.push(3); 
	myqueue.push(2); 
	myqueue.push(1); 

	// Queue becomes 1, 2, 3, 4, 5, 6, 7, 8 

	if (myqueue.front() > myqueue.back()) { 
		cout << myqueue.front() - myqueue.back(); 
	} 
	else if (myqueue.front() < myqueue.back()) { 
		cout << myqueue.back() - myqueue.front(); 
	} 
	else
		cout << "0"; 
} 

Output:

7

queue::push() and queue::pop() in C++ STL

Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.

queue::push()

push() function is used to insert an element at the back of the queue. The element is added to the queue container and the size of the queue is increased by 1. 函数push在队尾插入元素,队列的大小加1。

Syntax :

queuename.push(value)
Parameters :
The value of the element to be inserted is passed as the parameter.
Result :
Adds an element of value same as that of 
the parameter passed at the back of the queue.

Examples:

Input :  myqueue
         myqueue.push(6);
Output : 6

Input :  myqueue
         myqueue.push(0);
         myqueue.push(1);
Output : 0, 1

Errors and Exceptions

  1. Shows error if the value passed doesn’t match the queue type.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.
// CPP program to illustrate 
// Implementation of push() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	// Empty Queue 
	queue<int> myqueue; 
	myqueue.push(0); 
	myqueue.push(1); 
	myqueue.push(2); 

	// Printing content of queue 
	while (!myqueue.empty()) { 
		cout << ' ' << myqueue.front(); 
		myqueue.pop(); 
	} 
} 

Output:

0 1 2

queue::pop()

pop() function is used to remove an element from the front of the queue(oldest element in the queue). The element is removed to the queue container and the size of the queue is decreased by 1. 函数pop删除队头的元素。队列元素个数减1.

Syntax :

queuename.pop(value)
Parameters :
No parameters are passed
Result :
Removes the oldest element in the queue
or basically the front element.

Examples:

Input :  myqueue = 1, 2, 3
         myqueue.pop();
Output : 2, 3

Input :  myqueue = 3, 2, 1
         myqueue.pop();
Output : 2, 1

Errors and Exceptions

  1. Shows error if a parameter is passed.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.
// CPP program to illustrate 
// Implementation of pop() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	// Empty Queue 
	queue<int> myqueue; 
	myqueue.push(0); 
	myqueue.push(1); 
	myqueue.push(2); 
	// queue becomes 0, 1, 2 

	myqueue.pop(); 
	myqueue.pop(); 
	// queue becomes 2 

	// Printing content of queue 
	while (!myqueue.empty()) { 
		cout << ' ' << myqueue.front(); 
		myqueue.pop(); 
	} 
} 

Output:

2

Application : push() and pop()
Given a number of integers, add them to the queue and find the size of the queue without using size function.

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm
1. Push the given elements to the queue container one by one.
2. Keep popping the elements of queue until the queue becomes empty, and increment the counter variable.
3. Print the counter variable.

// CPP program to illustrate 
// Application of push() and pop() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	int c = 0; 
	// Empty Queue 
	queue<int> myqueue; 
	myqueue.push(5); 
	myqueue.push(13); 
	myqueue.push(0); 
	myqueue.push(9); 
	myqueue.push(4); 
	// queue becomes 5, 13, 0, 9, 4 

	// Counting number of elements in queue 
	while (!myqueue.empty()) { 
		myqueue.pop(); 
		c++; 
	} 
	cout << c; 
} 

Output:

5

queue::empty() and queue::size() in C++ STL

Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.

queue::empty()

empty() function is used to check if the queue container is empty or not. 判断队列是否为空。

Syntax :

queuename.empty()
Parameters :
No parameters are passed
Returns :
True, if list is empty
False, Otherwise

Examples:

Input :  myqueue = 1, 2, 3
         myqueue.empty();
Output : False

Input :  myqueue
         myqueue.empty();
Output : True

Errors and Exceptions

  1. Shows error if a parameter is passed
  2. Shows no exception throw guarantee.
// CPP program to illustrate 
// Implementation of empty() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<int> myqueue; 
	myqueue.push(1); 

	// Queue becomes 1 

	if (myqueue.empty()) { 
		cout << "True"; 
	} 
	else { 
		cout << "False"; 
	} 
	return 0; 
} 

Output:

False

Application : Given a queue of integers, find the sum of the all the integers.

Input  : 1, 8, 3, 6, 2
Output : 20

Algorithm
1. Check if the queue is empty, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the queue is empty.
3. Print the final value of the variable.

// CPP program to illustrate 
// Application of empty() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	queue<int> myqueue; 
	myqueue.push(1); 
	myqueue.push(8); 
	myqueue.push(3); 
	myqueue.push(6); 
	myqueue.push(2); 

	// Queue becomes 1, 8, 3, 6, 2 

	while (!myqueue.empty()) { 
		sum = sum + myqueue.front(); 
		myqueue.pop(); 
	} 
	cout << sum; 
	return 0; 
} 

Output:

20

queue::size()

size() function is used to return the size of the list container or the number of elements in the list container.获得元素的个数。

Syntax :

queuename.size()
Parameters :
No parameters are passed
Returns :
Number of elements in the container

Examples:

Input :  myqueue = 1, 2, 3
         myqueue.size();
Output : 3

Input :  myqueue
         myqueue.size();
Output : 0

Errors and Exceptions

  1. Shows error if a parameter is passed.
  2. Shows no exception throw guarantee
// CPP program to illustrate 
// Implementation of size() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	queue<int> myqueue; 
	myqueue.push(1); 
	myqueue.push(8); 
	myqueue.push(3); 
	myqueue.push(6); 
	myqueue.push(2); 

	// Queue becomes 1, 8, 3, 6, 2 

	cout << myqueue.size(); 

	return 0; 
} 

Output:

5

Application : Given a queue of integers, find the sum of the all the integers.

Input  : 1, 8, 3, 6, 2
Output : 20

Algorithm
1. Check if the size of the queue is zero, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the queue size becomes 0.
3. Print the final value of the variable.

// CPP program to illustrate 
// Application of empty() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	queue<int> myqueue; 
	myqueue.push(1); 
	myqueue.push(8); 
	myqueue.push(3); 
	myqueue.push(6); 
	myqueue.push(2); 

	// Queue becomes 1, 8, 3, 6, 2 

	while (myqueue.size() > 0) { 
		sum = sum + myqueue.front(); 
		myqueue.pop(); 
	} 
	cout << sum; 
	return 0; 
} 

Output:

20

queue::emplace() in C++ STL

Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed.

queue::emplace()

This function is used to insert a new element into the queue container, the new element is added to the end of the queue.在队尾增加元素。
Syntax :

queuename.emplace(value)
Parameters :
The element to be inserted into the queue
is passed as the parameter.

Result :
The parameter is added to the
forward list at the end.

Examples:

Input  : myqueue{1, 2, 3, 4, 5};
         myqueue.emplace(6);
Output : myqueue = 6, 1, 2, 3, 4, 5

Input  : myqueue{};
         myqueue.emplace(4);
Output : myqueue = 4

Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. The parameter should be of the same type as that of the container otherwise, an error is thrown.

// INTEGER queue EXAMPLE 
// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<int> myqueue; 
	myqueue.emplace(1); 
	myqueue.emplace(2); 
	myqueue.emplace(3); 
	myqueue.emplace(4); 
	myqueue.emplace(5); 
	myqueue.emplace(6); 

	// queue becomes 1, 2, 3, 4, 56 

	while (!myqueue.empty()) { 
		cout << ' ' << myqueue.front(); 
		myqueue.pop(); 
	} 

	return 0; 
	
} 

Output:

1 2 3 4 5 6
// CHARACTER QUEUE EXAMPLE 
// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<char> myqueue; 
	myqueue.emplace('k'); 
	myqueue.emplace('j'); 
	myqueue.emplace('y'); 
	myqueue.emplace('r'); 
	myqueue.emplace('y'); 
	myqueue.emplace('u'); 

	// queue becomes k, j, y, r, y, u 

	while (!myqueue.empty()) { 
		cout << ' ' << myqueue.front(); 
		myqueue.pop(); 
	} 

	return 0; 
	
} 

Output:

k j y r y u
// STRING QUEUE EXAMPLE 
// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <queue> 
using namespace std; 

int main() 
{ 
	queue<string> myqueue; 
	myqueue.emplace("This"); 
	myqueue.emplace("is"); 
	myqueue.emplace("a"); 
	myqueue.emplace("computer"); 
	myqueue.emplace("science"); 
	myqueue.emplace("portal"); 

	// queue becomes This, is, a, computer, 
	//science, portal 

	while (!myqueue.empty()) { 
		cout << ' ' << myqueue.front(); 
		myqueue.pop(); 
	} 

	return 0; 
	
} 

Output:

This is a computer science portal

Time Complexity : O(1)

Application : Input an empty queue and find the sum of the elements of the queue.

Input :  7, 6, 4, 2, 7, 8
Output : 34 

Algorithm
1. Insert elements into the queue using emplace() function.
2. Check if a queue is empty, if not add the front element to the sum variable and pop it.
3. Keep repeating this step until the queue becomes empty
4. Print the sum variable.

// CPP program to illustrate 
// application Of emplace() function 
#include <queue> 
#include <iostream> 
using namespace std; 

int main() 
{ 
	// variable declaration 
	int sum = 0; 
	
	// queue declaration 
	queue<int> myqueue{}; 
	
	// adding elements to the queue 
	myqueue.emplace(7); 
	myqueue.emplace(6); 
	myqueue.emplace(4); 
	myqueue.emplace(2); 
	myqueue.emplace(7); 
	myqueue.emplace(8); 

	// queue becomes 7, 6, 4, 2, 7, 8 

	// calculating the sum 
	while (!myqueue.empty()) { 
		sum = sum + myqueue.front(); 
		myqueue.pop(); 
	} 
	cout<< sum; 
} 

Output

34

queue::swap() in C++ STL

Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed.

queue::swap()
swap() function is used to exchange the contents of two queues but the queues must be of same type, although sizes may differ.交换两个队列的元素,类型必须一样。大小可不同。

Syntax:

queue1.swap(queue2)
            OR
swap(queue1, queue2)

Parameters:
queue1 is the first queue object.
queue2 is the second queue object.

Return value: None

Examples:

Input :  queue1 = {1, 2, 3, 4}
         queue2 = {5, 6, 7, 8}
         queue1.swap(queue2);
Output : queue1 = {5, 6, 7, 8}
         queue2 = {1, 2, 3, 4}

Input  : queue1 = {'a', 'b', 'c', 'd', 'e'}
         queue2 = {'f', 'g', 'h', 'i'}
         queue1.swap(queue2);
Output : queue1 = {'f', 'g', 'h', 'i'}
         queue2 = {'a', 'b', 'c', 'd', 'e'}

Errors and Exceptions

1. It throws an error if the queues are not of the same type.
2. It throws error if the queues are not of the same size.
2. It has a basic no exception throw guarantee otherwise.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

// CPP program to illustrate 
// Implementation of swap() function 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Take any two queues 
	queue<char> queue1, queue2; 
	int v = 96; 
	for (int i = 0; i < 5; i++) { 
	queue1.push(v + 1); 
	v++; 
	} 

for (int i = 0; i < 4; i++) { 
	queue2.push(v + 1); 
	v++; 
	} 
		
	// Swap elements of queues 
	queue1.swap(queue2); 

	// Print the first queue 
	cout << "queue1 = "; 
	while (!queue1.empty()) { 
	cout << queue1.front() << " "; 
	queue1.pop(); 
} 

	// Print the second set 
	cout << endl << "queue2 = "; 
	while (!queue2.empty()) { 
	cout << queue2.front() << " "; 
	queue2.pop(); 
} 
		
	return 0; 
} 

Output:

queue1 = f g h i 
queue2 = a b c d e 

Time complexity: Linear i.e. O(n)

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86534966