Last Updated:

Example of creating a dynamic array

A dynamic array is one that is capable of changing its size at any time. This capability is provided by dynamically allocating memory to the array. It is convenient to create a class that is a wrapper for this array, is responsible for allocating and releasing memory for the array, and also provides access to the elements of the array.

When a user creates a wrapper class object, the class constructor allocates memory to an array that has either a user-specified size or a default size. If, as the array fills, all the allocated memory is occupied, then when the next element is added, the previously allocated memory is released, all values stored in the array are stored in the temporary array. Then, memory is allocated for a larger array and the stored values are placed in it. Thus, changing the size of the array occurs automatically, invisible to the user.

Currently, there is practically no need to use such dynamic arrays, since such data structures, called containers, are presented in a large assortment in the standard library of C++ templates - Standard template library (STL), which is supported by almost all compilers. The most typical example is the vector class, which contains all the necessary functions and iterators. Also useful are container classes such as map, multymap, queue, deque, list, set, multyset.

Short description:

  • map - an associative array that contains key-value pairs, provides access to a value by key.
  • multymap is an associative array in which the same value keys can occur.
  • queue - queue, i.e. an array organized according to the FIFO ("first in first out") principle
  • deque - very similar to vector; as well as vector, it supports random access to elements, but does not support some of the functions that are present in the vector class.
  • list - a list that does not support access to the item by index; instead, the item is searched by value.
  • set - a set (or a simple associative array) that differs from an associative array in that in it the key is both a value (has an interface resembling a map interface, which allows you to painlessly alternate them)
  • multyset - a set in which the same value keys can occur.

The C++ program below does not implement all possible and useful functions for working with dynamic arrays, because it is created only to demonstrate the principle of creating such arrays.

Class Definition:

template<class T>
class DynamicArray {
      long size; //size of the array
      long count; //number of elements in the array
      T*p; //pointer to the beginning of the array

  public:
      //constructors
      DynamicArray(long s = 10): size(s), count(0) {
          p = new T[size];
          if(!p)
             cout << "Error creating array" << endl;
      }
      DynamicArray(const DynamicArray& arr); // copy constructor

      // destructor
      ~DynamicArray() {
          if(p) delete[]p;
      }

      //functions
      void add(Tx); //add an element to the end of the array
      void remove(); //remove the last element
      long length() const {return size;} //array length
      void print() const; //output on display

      //operators
      DynamicArray& operator=(const DynamicArray& arr); //assignment
      T operator [] (long i) const; //indexing
      DynamicArray& operator+(const DynamicArray& arr); //addition
};

Implementation of functions and operators:

//copy constructor
template<class T>
DynamicArray<T>::DynamicArray(const DynamicArray& arr) {
     size = arr.size;
     count = arr.count;
     p = new T[size];
     for(int i = 0; i<count; ++i)
         p[i] = arr.p[i];
}

Here we return the value by reference so that you can build a chain of assignments:

template<class T>
DynamicArray<T>&
DynamicArray<T>::operator=(const DynamicArray& arr)
      if(this != &arr) { //to avoid self-assignment
         size = arr.size;
         count = arr.count;
         if(p) delete[]p;
         p = new T[size];
         for(int i = 0; i<count; ++i)
            p[i] = arr.p[i];

     }
     return *this;
}

template<class T>
T DynamicArray<T>::operator[](long i) const {
    if(i < size && i)
        return p[i-1];
    else
        cout << "Invalid index" << endl;
    return 0;
}

template<class T>
DynamicArray<T>&
DynamicArray<T>::operator+(const DynamicArray& arr) {
    DynamicArray temp(*this); //store values ​​in temporary array
    if(p) delete[]p;
    size += arr.size;
    count += arr.count;
    p = new T[size];
    for(int i = 0; i<temp.count; ++i)
         p[i] = temp.p[i];
    for(int i = 0; i<arr.count; ++i)
         p[temp.count + i] = arr.p[i];
    return *this;
}

template<class T>
void DynamicArray<T>::print() const {
    cout << "The array contains:" << endl;
    for(int i = 0; i<count; ++i)
         cout << p[i] << ' ';
    cout << endl;
}

template<class T>
void DynamicArray<T>::add(Tx) {
    if(count >= size) {
        //increase the size of the array
        DynamicArray temp(*this);
        if(p) delete[]p;
        size += 10;
        p = new T[size];
        for(int i = 0; i<temp.count; ++i)
            p[i] = temp.p[i];
    }
    p[count++] = x; //add an element to the end of the array
}

template<class T>
void DynamicArray<T>::remove() {
   if(count)
       p[--count] = 0; //remove the last element (if the array
                          //not empty)
   else
       cout << "Array is empty" << endl;
}