How to Enqueue an Element into a Queue in C++? Last Updated : 23 Jul, 2025 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) Create Quiz Comment D denzirop9v Follow 0 Improve D denzirop9v Follow 0 Improve Article Tags : C++ Programs C++ STL cpp-queue CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like