GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Queue in C++ STL - GeeksforGeeks Queue in C++ STLLast Updated : 31 Aug, 2026 A queue is a container adapter that stores elements in FIFO (First In, First Out) order. It allows elements to be inserted from the back and removed from the front, ensuring the first inserted element is removed first.
![]() Creating a Queue: A queue is defined in the <queue> header file and can be created using the std::queue class template.
Output Front element: 10 Back element: 5 Front element after pop: 5 Explanation: The program creates a queue and adds 10 and 5 using push(). The front() and back() functions access the first and last elements, respectively. The pop() function removes 10 from the front, making 5 the new front element. Syntax
where,
Basic Operations on QueueThe commonly used operations on a queue are discussed below. 1. Inserting ElementsThe push() function inserts an element at the back of the queue. This operation is also known as enqueue.
Explanation: This code creates a queue and inserts three elements using the push() function. The elements are added to the back of the queue in the order 3, 4, 5. 2. Accessing ElementsThe front() and back() functions are used to access the front and back elements of a queue.
Output Front: 3 Back: 5 Explanation: This code inserts three elements into the queue and uses front() to access the first element and back() to access the last element. 3. Deleting ElementsThe pop() function removes the front element of the queue. This operation is also known as dequeue.
Output 4 5 Explanation: The code first inserts 3, 4, and 5 into the queue. The pop() function removes 3, which is the front element. The while loop then prints and removes the remaining elements one by one. 4. Checking if Queue is EmptyThe empty() function checks whether the queue contains any elements.
Output Queue is empty Queue is not empty. Front element: 100 Explanation: The code first checks an empty queue using empty(). After inserting 100, it checks the queue again and confirms that it is no longer empty. 5. Finding the Size of QueueThe size() function returns the number of elements currently present in the queue.
Output Size of queue: 2 Size of queue: 1 Explanation: The code inserts two elements into the queue, so size() initially returns 2. After removing one element using pop(), the queue contains one element. Traversing a QueueA queue does not provide iterators, so its elements cannot be directly traversed like a vector or list. To inspect all elements without modifying the original queue, we can create a copy and repeatedly access and remove its front element.
Output 3 4 5 Explanation: The code creates a copy of the original queue and traverses the copy using front() and pop(). Since the copy is modified instead of the original queue, the elements of q remain unchanged. Member Functions of std::queueThe following are the commonly used member functions of std::queue:
Comment ExploreStandard Template Library(STL) Practice & Problems | ||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |