Join our WhatsApp Channel
Cart / 0.00$

No products in the cart.

Visit Support Portal
No Result
View All Result
Sunday, July 26, 2026
  • Home
  • Tutorials
    • Machine Learning
    • Python
    • Web Development
    • Javascript
    • Mathematics
    • Deep Learning
    • Artificial Intelligence
  • Store
  • Our ServicesBook Now
    • Managed Hosting Services
    • Web Development Services
  • Our Softwares
    • JaggoRe AIFree
    • Email ExtractorPaid
    • TimeWell AppFree
Get a Free Quote
  • Login
  • Register
Neuraldemy
Sunday, July 26, 2026
Cart / 0.00$

No products in the cart.

No Result
View All Result
Neuraldemy
Get A Free Quote
Home C++

Dynamic Array Implementation Modern C++

Neuraldemy by Neuraldemy
January 20, 2026
in C++
Reading Time: 5 mins read
A A
Dynamic Array Implementation Modern C++

In this post, we’ll walk through a simplified implementation of a dynamic array class called MyVector. It mirrors many of the behaviors of std::vector while staying small enough to reason about and explain in an interview or learning context.

#include <cstddef>
#include <utility>
#include <stdexcept>
#include <iostream>
#include <string>

template<typename T>
class MyVector {
    private:
      T* data;
      size_t capacity;
      size_t length;

      void resize() {
          size_t newCapacity = (capacity == 0) ? 1 : capacity * 2;
          T* newData = new T[newCapacity];

          for(size_t i = 0; i < length; i++) {
              newData[i] = std::move(data[i]);
          }

          delete[] data;
          data = newData;
          capacity = newCapacity;
      }

      public:
        MyVector() : data(nullptr), capacity(0), length(0) {}

        ~MyVector() {
            delete[] data;
        }

        MyVector(const MyVector& other) : data(nullptr), capacity(other.capacity), length(other.length) {
              if(capacity > 0) {
                  data = new T[capacity];
                  for(size_t i = 0; i < length; i++) {
                      data[i] = other.data[i];
                  }
              }
        }

        MyVector(MyVector&& other) noexcept
            : data(other.data), capacity(other.capacity), length(other.length) {

            other.data = nullptr;
            other.capacity = 0;
            other.length = 0;
        }

        MyVector& operator=(const MyVector& other) {
            if(this != &other) {
                T* newData = nullptr;
                if(other.capacity > 0) {
                    newData = new T[other.capacity];
                    for(size_t i = 0; i < other.length; i++) {
                        newData[i] = other.data[i];
                    }
                }

                delete [] data;
                data = newData;
                capacity = other.capacity;
                length = other.length;
            }
            return *this;
        }

        MyVector& operator=(MyVector&& other) noexcept {

            if(this != &other) {
                delete [] data;

                data = other.data;
                capacity = other.capacity;
                length = other.length;

                other.data = nullptr;
                other.capacity = 0;
                other.length = 0;
            }
            return *this;
        }

       void push(const T& value) {
           if(length == capacity) {
               resize();
           }
           data[length++] = value;
       }

       void push(T&& value) {
           if (length == capacity) {
               resize();
           }
           data[length++] = std::move(value);
       }

       T& operator[](size_t index){
           return data[index];
       }

       const T& operator[](size_t index) const {
           return data[index];
       }

       size_t size() const {
           return length;
       }

       size_t getCapacity() const {
              return capacity;
          }

          void clear() {
                  length = 0;
              }

       T& at(size_t index) {
           if(index >= length) {
               throw std::out_of_range("Index out of range");
           }
           return data[index];
       }

       const T& at(size_t index) const {
           if(index >= length) {
               throw std::out_of_range("Index out of range");
           }
           return data[index];
       }

       void reserve(size_t newCapacity) {
           if(newCapacity <= capacity) {
               return;
           }

           T* newData = new T[newCapacity];

           for(size_t i=0; i <length; i++) {
               newData[i] = std::move(data[i]);
           }

           delete [] data;
           data = newData;
           capacity = newCapacity;
       }
};


int main() {
    MyVector<int> v;

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

    std::cout << "v size: " << v.size() << "\n";
    std::cout << "v capacity: " << v.getCapacity() << "\n";

    std::cout << "Elements:\n";
    for (size_t i = 0; i < v.size(); i++) {
        std::cout << v[i] << " ";
    }
    std::cout << "\n";

    // Copy constructor
    MyVector<int> v2 = v;
    v2[0] = 100;

    std::cout << "v2[0] after copy: " << v2[0] << "\n";
    std::cout << "v[0] original: " << v[0] << "\n";

    // Copy assignment
    MyVector<int> v3;
    v3 = v;

    // Move constructor
    MyVector<int> v4 = std::move(v3);

    // Move assignment
    MyVector<int> v5;
    v5 = std::move(v4);

    // at() with bounds checking
    try {
        std::cout << "v5.at(1): " << v5.at(1) << "\n";
        std::cout << v5.at(10) << "\n"; // will throw
    } catch (const std::out_of_range& e) {
        std::cout << "Exception: " << e.what() << "\n";
    }

    // reserve()
    MyVector<std::string> vs;
    vs.reserve(5);
    vs.push("hello");
    vs.push("world");

    std::cout << "vs capacity after reserve: " << vs.getCapacity() << "\n";
    std::cout << "vs[0]: " << vs[0] << "\n";

    // clear()
    vs.clear();
    std::cout << "vs size after clear: " << vs.size() << "\n";

    return 0;
}

Support

Buy author a coffee

Support
SummarizeSendShareTweetScan
Previous Post

How to Fix RangeError: Maximum Call Stack Size Exceeded in React & Next.js

Next Post

Singleton pattern In JavaScript

Neuraldemy

Neuraldemy

This is Neuraldemy support. Subscribe to our YouTube channel for more.

Related Posts

Git Tutorial: A Beginners Guide To Git And GitHub

Why RAM Prices Will Continue To Rise?

100 CSS Interview Questions

100 CSS Interview Questions

FormData API: A Beginner’s Guide to Native Validation

Strategy Pattern In JavaScript

Factory Pattern In JavaScript

Next Post
Singleton pattern In JavaScript

Singleton pattern In JavaScript

Close-up of a smartphone displaying ChatGPT app held over AI textbook.

The 5 Best Books for Learning Machine Learning Mathematics

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

Support

Buy author a coffee

Support

Support Neuraldemy

Newsletter

Top rated products

  • Probability and Statistics for Machine Learning and Data Science Probability and Statistics for Machine Learning and Data Science
    Rated 5.00 out of 5
    30.00$ Original price was: 30.00$.12.99$Current price is: 12.99$.
  • SVM Notes: Optimization & Implementation SVM Notes: Optimization & Implementation
    Rated 5.00 out of 5
    9.99$ Original price was: 9.99$.4.99$Current price is: 4.99$.
  • spidy mail hunter software Spidy Mail Hunter: Powerful And Intelligent Website & PDF Email Extractor Software (Windows App)
    Rated 4.78 out of 5
    149.00$ Original price was: 149.00$.49.00$Current price is: 49.00$.
  • Linear Algebra For Machine Learning And Data Science Linear Algebra For Machine Learning And Data Science
    Rated 4.71 out of 5
    40.00$ Original price was: 40.00$.24.99$Current price is: 24.99$.
  • Clustering and Outlier Detection A Comprehensive Tutorial Clustering and Outlier Detection 20.97$ Original price was: 20.97$.13.47$Current price is: 13.47$.

Recent Posts

Git Tutorial: A Beginners Guide To Git And GitHub

Why RAM Prices Will Continue To Rise?

100 CSS Interview Questions

100 CSS Interview Questions

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Mar    

Neuraldemy

Neuraldemy

Neuraldemy

Software Development

Neuraldemy helps you learn ML, AI, Web Dev and data science from scratch. Addtionally, we also provide web development and hosting services for businesses, and individuals

  • Get Started
  • Contact
  • Book Services
  • Privacy Policy
  • Terms Of Use
  • Support Portal
  • Managed Hosting Terms
  • Web Development Terms
  • Refund Policy
Neuraldemy

© 2026 - A learning platform by Odist Magazine

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
Join Our WhatsApp Channel
  • Home
  • Tutorials
    • Machine Learning
    • Python
    • Web Development
    • Javascript
    • Mathematics
    • Deep Learning
    • Artificial Intelligence
  • Store
  • Our Services
    • Managed Hosting Services
    • Web Development Services
  • Our Softwares
    • JaggoRe AI
    • Email Extractor
    • TimeWell App
  • Login
  • Sign Up
  • Cart
Order Details

© 2026 - A learning platform by Odist Magazine

This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
0