dynamic Array in C++

2
3552
Overloading steam insertion and extraction operators in C++
Fibonacci Number program in C++ using Recursion

Dynamic Arrays also Called Array List in C++ are the ones with random size, which can be expanded if needed and contracted if needed. Dynamic Arrays in C++ have the Following Specs:

  1. Index Automatically increases if the data is inserted at all indexes.
  2. User can erase a Specific Index or data at that Index.
  3. Capacity of the Array Can easily be Increased.
  4. Size of Array Can be Increased or decreased Manually.

The Code for Dynamic Arrays in C++ is Below or You can Download the SourceCode Here:

download

//Implement array list
#include<iostream>
using namespace std;

template <class T>
	class Vector
	{
	private:
		int size,capacity;
		T *obj;
	public:
		Vector()
		{
			size = capacity = 0;
			obj = NULL;
		}

		~Vector()
		{
			delete [] obj;
		}
		Vector(int a)
		{
			size = a;
			obj = new T[size];

			for(int i = 0 ; i<size; i++)
			{
				obj[i] = NULL;
			}
		}
		int size_of_list();
		bool empty();
		void reserve(int);
		T& operator[](int);
		T at(int);
		T front();
		T back();
		void push_back(T element);
		void pop_back();
		void insert(int,T);
		void erase(int);
	
	};

template<class T>
int Vector<T>::size_of_list()
{
	return size;
}

template<class T>
bool Vector<T>::empty()
{
	if(size == 0)
		return true;
	else
		return false;
}

template<class T>
void Vector<T>::reserve(int Num)
{
	if(capacity >Num)
		return;
	T *temp = new T[Num];
	for(int i = 0; i<size; i++)
	{
		temp[i] = obj[i];
	}
	if(obj != NULL) delete [] obj;

	obj = temp;
	capacity = Num;
}

template<class T>
T& Vector<T>::operator[](int index)
{
	return obj[index];
}

template<class T>
T Vector<T>::at(int i)
{
	return obj[i];
}

template<class T>
T Vector<T>::front()
{
	return obj[0];
}

template<class T>
T Vector<T>::back()
{
	return obj[size - 1];
}

template<class T>
void Vector<T>::insert(int i, T e)
{
	obj[i] = e;
}

template<class T>
void Vector<T>::erase(int i)
{
	if(i>=0 && i<size)
	{
		for(int j = 0; j<size; j++)
		{
			obj[j] = obj[j+1];
		}
		obj[size] = NULL;
	}
}

int main()
{
	Vector<int> a(3);
	a.insert(0,1);
	a.insert(1,2);
	a.insert(2,3);

	//retrieving data
	cout<<a.at(0)<<endl;
	cout<<a.at(1)<<endl;
	cout<<a.at(2)<<endl;
	cout<<"the size of vector is : ";
	cout<<a.size_of_list()<<endl;// getting size
	a.reserve(10);// Increasing capacity

	//erasing and then checking for garbage
	a.erase(2);
	cout<<a.at(2)<<endl;

	


	return 0;
}

A class Vector is Created first and then All the functions for insert,erase,reserve etc. are created for the dynamic array in C++ as commented in the code.You can refer to the comments if there is an issue.

Overloading steam insertion and extraction operators in C++
Fibonacci Number program in C++ using Recursion

2 COMMENTS

    • You can try it.
      But it might not work, because “i” is the index that you want to erase and “j” in the loop index which is further used to adjust the array after erase .

Leave a Reply