Queue in C++ STL (original) (raw)

Last Updated : 11 May, 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.

dequeue_operation_in_queue_1

Queue in C++

C++ `

#include #include using namespace std;

int main() { queue q; q.push(10); q.push(5);

// Accessing the front and back elements
cout << "Front element: " << q.front() << endl;
cout << "Back element: " << q.back() << endl;

// Removing an element from the front
q.pop();

cout << "Front element after pop: " << q.front() << endl;
return 0;

}

`

Output

Front element: 10 Back element: 5 Front element after pop: 5

Syntax

Queue is defined as the std::queue class template inside header file.

queue q;

where,

Basic Operations

Here are the basic operations that can be performed on a queue:

Inserting Elements

#include #include using namespace std;

int main(){ queue q;

// Pushing elements into the queue
q.push(3);
q.push(4);
q.push(5);

return 0;

}

`

Accessing Elements

Only the front and back elements of the queue can be accessed by using front() and back() functions respectively.

C++ `

#include #include using namespace std;

int main() { queue q; q.push(3); q.push(4); q.push(5);

// Accessing the front and back elements
cout << q.front() << endl;
cout << q.back();

return 0;

}

`

Deleting Elements

#include #include using namespace std;

int main(){ queue q; q.push(3); q.push(4); q.push(5);

// Deleting elements from the front of the queue
q.pop();

while (!q.empty()){
    cout << q.front() << " ";
    q.pop();
}
return 0;

}

`

**Note: In a queue, elements can only be inserted at the back and removed from the front. It does not provide access to middle elements, so deleting an element from the middle is not directly possible.

empty()

#include #include using namespace std;

int main(){

queue<int> q;
if (q.empty()){
    cout << "Queue is empty " << endl;
}
q.push(100);
if (!q.empty()){
    cout << "Queue is not empty. Front element: " << q.front() << endl;
}
return 0;

}

`

Output

Queue is empty Queue is not empty. Front element: 100

Size of queue

#include #include using namespace std;

int main() {

queue<int> q;
q.push(10);
q.push(5);
cout << "Size of queue: " << q.size() << endl;
q.pop();
cout << "Size of queue: " << q.size() << endl;
return 0;

}

`

Output

Size of queue: 2 Size of queue: 1

Pseudo Traversal

#include #include using namespace std;

int main(){ queue q; q.push(3); q.push(4); q.push(5);

// Create a copy
queue<int> temp(q);

while (!temp.empty())
{
    cout << temp.front() << " ";
    temp.pop();
}
return 0;

}

`

Try It Yourselfredirect icon

All Member Functions

Following is the list of all member functions of std::queue class in C++:

**Functions **Description
front() Access the front element of the queue.
back() Access the end element of the queue.
empty() Check whether a queue is empty or not.
size() Returns the number of elements in the queue.
push() Adding an element at the back of the queue.
**push_range() Adding multiple elements at the end of queue.
**emplace() Constructs and inserts an element at the back of the queue.
pop() Delete the front element of the queue.
swap() Swap two queues.