How to Enqueue an Element into a Queue in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL, we have a queue container that simulates the queue data structure and follows the FIFO (First In, First Out) rule. In this article, we will learn how to enqueue (insert) an element into a queue in C++. Example: Input: myQueue = {10, 20, 30}; elementToEnqueue= 40 Output: Queue after enqueue: 10 20 30 40 Add an Element to a Queue in C++To enqueue an element into a std::queue in C++ STL, we can use the std::queue::push() function that inserts a new element at the end of the queue, after its current last element. C++ Program to Enqueue an Element into a Queue The below example demonstrates how we can enqueue an element into a queue in C++. C++ // C++ program to illustrate how to enqueue an element into // a queue #include <iostream> #include <queue> using namespace std; int main() { // Creating a queue of integers queue<int> myQueue; myQueue.push(10); myQueue.push(20); myQueue.push(30); // enqueue an element in a queue int elementToEnqueue = 40; myQueue.push(elementToEnqueue); // Displaying the queue elements cout << "Elements in a Queue are:" << endl; while (!myQueue.empty()) { cout << myQueue.front() << " "; myQueue.pop(); } cout << endl; return 0; } OutputElements in a Queue are: 10 20 30 40 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Create a Stack of Queue in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-queue CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Dequeue an Element from a Queue in C++? In C++, queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. In this article, we will learn how to dequeue an element from a queue in C++ STL. Example: Input: Create a queue an 2 min read How to Delete an Element from a Priority Queue in C++ ? In C++, a priority queue is a queue in which elements are arranged based on their priority values as each element has a priority value associated with it. In this article, we will learn how to delete an element from a priority queue in C++. For Example, Input: myPriorityQueue = {8, 6, 7, 5, 4, 2, 3} 2 min read How to Push All Elements from a Vector to a Queue in C++? In C++, vectors are dynamic arrays while the queue is a data structure that follows the FIFO (First In First Out) property. In this article, we will learn how to push all elements from a vector to a queue in C++. Example Input: myVector = {1, 5, 8, 9, 4} Output: myQueue = 1 5 8 9 4Copy All Vector El 2 min read How to Create a Stack of Queue in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::queue is a container that follows the FIFO (First In, First Out) rule. In this article, we will learn how to create a stack of a queue in C++. Example: Input:myQueue = { a, b, c };myQueue = { d, e };O 2 min read How to Access the Last Element of a Deque in C++? In C++ STL, we have a deque container which is a double-ended queue that allows us to add or remove elements from both ends. In this article, we will learn how to access the last element in a deque in C++. For Example, Input:myDeque = {1, 2, 3, 4, 5, 6}Output: Last Element: 6Accessing the Last Eleme 2 min read How to Remove an Element from Front of Deque in C++? In C++, a deque (double-ended queue) is a data structure that allows efficient insertion and deletion at both ends. In this article, we will learn to remove an element from the beginning of a deque in C++. Example:Input: myDeque = {1, 2, 3, 4, 5}; Output: Deque After Removal: 2 3 4 5Removing the Fir 2 min read Like