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 Enqueue an Element into a 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 Add an Element at the End of a Deque in C++? In C++, a deque is a flexible data structure that supports the insertion and deletion of elements from both ends efficiently. In this article, we will learn how to add an element at the end of a deque in C++. Example: Input: myDeque ={10,20,30,40} Output: // added element 50 at the end myDeque ={10, 2 min read How to Find the Sum of All Elements in a Deque in C++? In C++, double-ended queues are sequence containers similar to vectors but are more efficient in the case of insertion and deletion of elements at both ends. In this article, we will learn how to find the sum of all elements in a deque in C++. Example Input: myDeque ={1,2,3,4,5} Output: Sum of all d 2 min read How to Remove an Element from the End of a Deque in C++? In C++, deque is also called a double-ended queue where we can insert new elements at both the front and back of the container. In this article, we will learn to remove an element from the end of a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5} Output: Deque after removal: 1 2 3 4 Removing 2 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Find First Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers with the feature of insertion and deletion on both ends. In this article, we will learn how to find the first occurrence of a specific element in a deque in C++. Example Input: myDeque ={2, 1, 5, 3, 4, 2, 5} Target=5 Output: Th 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 Like