How to implement our own Vector Class in C++?

Last Updated : 28 May, 2026

A vector in C++ is a dynamic array that can automatically resize itself when elements are inserted or removed. It provides contiguous memory storage along with efficient random access using indexing.

  • A vector automatically increases its capacity when the current storage becomes full, ensuring smooth insertion of new elements.
  • Elements are stored in contiguous memory locations, which allows fast access using indices similar to arrays.
  • Insertion at the end is highly efficient with amortized O(1) time complexity, while deletion from the end is also constant time.

Functions Supported in Custom Vector Class

We can make our vector implementation more flexible by using templates, which allows it to work with any data type. The following are the key functions that we will implement in our custom vector class:

  • void push(int data): This function takes one element and inserts it at the last. Amortized time complexity is O(1).
  • void push(int data, int index): It inserts data at the specified index. Time complexity is O(1).
  • int get(int index): It is used to get the element at the specified index. Time complexity is O(1).
  • void pop(): It deletes the last element. Time complexity is O(1).
  • int size(): It returns the size of the vector i.e, the number of elements in the vector. Time complexity is O(1).
  • int getcapacity(): It returns the capacity of the vector. Time complexity is O(1).
  • void print(): It is used to print array elements. Time complexity is O(N), where N is the size of the vector.

Below is the implementation of our own Vector class. 

C++
#include <bits/stdc++.h>
using namespace std;

template <typename T>
class vectorClass {

    T* arr;
    int capacity;
    int current;

public:

    // Constructor
    vectorClass() {
        arr = new T[1];
        capacity = 1;
        current = 0;
    }

    // Destructor
    ~vectorClass() {
        delete[] arr;
    }

    // Push at end (with resizing)
    void push(T data) {

        if (current == capacity) {
            T* temp = new T[2 * capacity];

            for (int i = 0; i < capacity; i++) {
                temp[i] = arr[i];
            }

            delete[] arr;
            arr = temp;
            capacity *= 2;
        }

        arr[current] = data;
        current++;
    }

    // Insert at index
    void push(T data, int index) {

        if (index < 0 || index > current)
            return;

        if (current == capacity)
            push(data);

        for (int i = current; i > index; i--) {
            arr[i] = arr[i - 1];
        }

        arr[index] = data;
        current++;
    }

    // Safe get
    T get(int index) {
        if (index >= 0 && index < current)
            return arr[index];

        return T();
    }

    // Pop last element
    void pop() {
        if (current > 0)
            current--;
    }

    // Size
    int size() {
        return current;
    }

    // Capacity
    int getcapacity() {
        return capacity;
    }

    // Print elements
    void print() {
        for (int i = 0; i < current; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};

// Driver code
int main() {

    vectorClass<int> v;
    vectorClass<char> v1;

    v.push(10);
    v.push(20);
    v.push(30);
    v.push(40);
    v.push(50);

    v1.push('A');
    v1.push('B');
    v1.push('C');
    v1.push('D');

    cout << "Vector size: " << v.size() << endl;
    cout << "Vector capacity: " << v.getcapacity() << endl;

    cout << "Vector elements: ";
    v.print();

    v.push(100, 1);

    cout << "\nAfter inserting at index 1:\n";

    cout << "Int vector: ";
    v.print();

    cout << "Char vector: ";
    v1.print();

    cout << "Element at index 1 (int): " << v.get(1) << endl;
    cout << "Element at index 1 (char): " << v1.get(1) << endl;

    v.pop();
    v1.pop();

    cout << "\nAfter deleting last element:\n";

    cout << "Int vector size: " << v.size() << endl;
    cout << "Char vector size: " << v1.size() << endl;

    cout << "Int vector elements: ";
    v.print();

    cout << "Char vector elements: ";
    v1.print();

    return 0;
}

Output

Vector size: 5
Vector capacity: 8
Vector elements: 10 20 30 40 50

After inserting at index 1:
Int vector: 10 100 20 30 40 50
Char vector: A B C D
Element at index 1 (int): 100
Element at index 1 (char): B

After deleting last element:
Int vector size: 5
Char vector size: 3
Int vector elements: 10 100 20 30 40
Char vector elements: A B C
Comment