Implementing a Queue using a Vector in C++

Nitish Singh
4 min readJul 3, 2023

--

The standard queue is a container adaptor that provides the functionality of a queue, adhering to the first-in, first-out (FIFO) data structure. It follows the principle of “First in, First out(FIFO), where the first element added to the queue is the first one to be removed. The queue operates by pushing elements onto the back of the underlying container and popping them from the front.

In C++, the Standard Template Library (STL) provides a queue container that implements a queue. However, it’s also interesting and educational to build our own queue using vector data structures. In this article, we’ll explore the implementation of a queue using a vector template in C++.

Please note that the queue container utilizes a container that serves as a storage mechanism for holding its elements. The standard containers; “deque and list”; can meet these requirements and be employed as the underlying container for the queue.

mudamasters

Implementation of a Queue using a Vector

Vectors is a sequence container that encapsulates dynamic size arrays. The elements in a vector are stored contiguously, allowing access through iterators or regular pointers to elements. This means that a pointer to an element of a vector can be used in functions that expect a pointer to an element of an array.

The storage of the vector is managed automatically, and it will be expanded as needed. Vectors typically occupy more space than static arrays because they allocate extra memory to handle future growth. This allows the vector to avoid reallocation each time an element is inserted, only reallocating when the additional memory is exhausted. The total allocated memory can be determined using the capacity() function, and any excess memory can be released to the system using the shrink_to_fit() function.

In our implementation, we’ll utilize the vector container provided by the C++ STL. The vector allows us to easily add elements to the back and remove elements from the front, making it an excellent choice for implementing a queue.

Step 1:
Define the Queue template Class
Let’s start by defining our Queue template class. The template parameter T will represent the type of elements the queue will hold. We’ll declare a private member variable, elements, as a vector of type T, which will store the elements of the queue.

template <typename T>
class Queue {
private:
std::vector<T> elements;
// …
};

Step 2:
Enqueueing Elements
The enqueue operation adds an element to the back of the queue. We’ll simply use the push_back method provided by the vector to accomplish this:


void enqueue(const T& value) { elements.push_back(value); }

Step 3 & 4:
Checking if the Queue is Empty || Dequeueing Elements
To determine whether the queue is empty, we can utilize the empty method provided by the vector.
To remove an element from the front of the queue, we’ll implement the dequeue operation. Since we want to maintain the FIFO order, we’ll use the erase method to remove the first element from the vector if the queue is not empty

bool empty() const { return elements.empty(); }

void dequeue() {
if (!empty()) {
elements.erase(elements.begin());
}
}

Step 5:
Accessing the Front Element
The front operation allows us to access the front element of the queue without removing it. We’ll use the front method provided by the vector, but since accessing the front of an empty queue is an error, we’ll throw an exception in such cases:

const T& front() const {
if (!empty()) {
return elements.front();
}
throw std::out_of_range("Queue is empty");
}

Step 6:
Printing the Queue
For demonstration purposes, we’ll add a printQueue method that prints all the elements of the queue:

void printQueue() const {
for (const T& element : elements) {
std::cout << element << " ";
}
std::cout << std::endl;
}

Conclusion

In conclusion, this article discussed the implementation of a queue using a vector template in C++. The use of vector container and its dynamic array-like behavior provided a flexible and efficient way to implement the basic operations of a queue, such as enqueue and dequeue.

The implementation of a queue using a vector template not only helps in gaining a deeper understanding of queues but also showcases the power and flexibility of C++ templates. Templates are a useful tool for creating generic algorithms and data structures that can work with different data types.

More Reads

In order to enhance code reusability, it is imperative to have a thorough grasp of utilizing templates. This knowledge can be obtained by referring to the GitHub link.

If you’re looking to improve your understanding of C++, the C++ FAQ Lite is a great resource to turn to. This comprehensive guide contains a wealth of information on a wide range of topics related to the C++ programming language.

--

--

Nitish Singh
Nitish Singh

Written by Nitish Singh

Passionate about empowering devs with practical tips, using modern C++ as the tool, and up-to-date knowledge. Join me on Medium for engaging articles.

No responses yet