Friday, March 5, 2021
Home C++ Arrays dynamic Array in C++

dynamic Array in C++

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.

2 COMMENTS

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....