#ifndef INTARRAY_H#define INTARRAY_H#include<cassert> // for assert()classIntArray{private:intm_length{};int*m_data{};public:IntArray()=default;IntArray(intlength):m_length{length}{assert(length>=0);if(length>0)m_data=newint[length]{};}~IntArray(){delete[]m_data;// we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway}voiderase(){delete[]m_data;// We need to make sure we set m_data to nullptr here, otherwise it will// be left pointing at deallocated memory!m_data=nullptr;m_length=0;}int&operator[](intindex){assert(index>=0&&index<m_length);returnm_data[index];}// reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly.voidreallocate(intnewLength){// First we delete any existing elementserase();// If our array is going to be empty now, return hereif(newLength<=0)return;// Then we have to allocate new elementsm_data=newint[newLength];m_length=newLength;}// resize resizes the array. Any existing elements will be kept. This function operates slowly.voidresize(intnewLength){// if the array is already the right length, we're doneif(newLength==m_length)return;// If we are resizing to an empty array, do that and returnif(newLength<=0){erase();return;}// Now we can assume newLength is at least 1 element. This algorithm// works as follows: First we are going to allocate a new array. Then we// are going to copy elements from the existing array to the new array.// Once that is done, we can destroy the old array, and make m_data// point to the new array.// First we have to allocate a new arrayint*data{newint[newLength]};// Then we have to figure out how many elements to copy from the existing// array to the new array. We want to copy as many elements as there are// in the smaller of the two arrays.if(m_length>0){intelementsToCopy{(newLength>m_length)?m_length:newLength};// Now copy the elements one by onefor(intindex{0};index<elementsToCopy;++index)data[index]=m_data[index];}// Now we can delete the old array because we don't need it any moredelete[]m_data;// And use the new array instead! Note that this simply makes m_data point// to the same address as the new array we dynamically allocated. Because// data was dynamically allocated, it won't be destroyed when it goes out of scope.m_data=data;m_length=newLength;}voidinsertBefore(intvalue,intindex){// Sanity check our index valueassert(index>=0&&index<=m_length);// First create a new array one element larger than the old arrayint*data{newint[m_length+1]};// Copy all of the elements up to the indexfor(intbefore{0};before<index;++before)data[before]=m_data[before];// Insert our new element into the new arraydata[index]=value;// Copy all of the values after the inserted elementfor(intafter{index};after<m_length;++after)data[after+1]=m_data[after];// Finally, delete the old array, and use the new array insteaddelete[]m_data;m_data=data;++m_length;}voidremove(intindex){// Sanity check our index valueassert(index>=0&&index<m_length);// If we're removing the last element in the array, we can just erase the array and return earlyif(m_length==1){erase();return;}// First create a new array one element smaller than the old arrayint*data{newint[m_length-1]};// Copy all of the elements up to the indexfor(intbefore{0};before<index;++before)data[before]=m_data[before];// Copy all of the values after the removed elementfor(intafter{index+1};after<m_length;++after)data[after-1]=m_data[after];// Finally, delete the old array, and use the new array insteaddelete[]m_data;m_data=data;--m_length;}// A couple of additional functions just for conveniencevoidinsertAtBeginning(intvalue){insertBefore(value,0);}voidinsertAtEnd(intvalue){insertBefore(value,m_length);}intgetLength()const{returnm_length;}};#endif
#include<iostream>#include"IntArray.h"intmain(){// Declare an array with 10 elementsIntArrayarray(10);// Fill the array with numbers 1 through 10for(inti{0};i<10;++i)array[i]=i+1;// Resize the array to 8 elementsarray.resize(8);// Insert the number 20 before element with index 5array.insertBefore(20,5);// Remove the element with index 3array.remove(3);// Add 30 and 40 to the end and beginningarray.insertAtEnd(30);array.insertAtBeginning(40);// Print out all the numbersfor(inti{0};i<array.getLength();++i)std::cout<<array[i]<<' ';std::cout<<'\n';return0;}