C ++ realize dynamic variable length array

       Overview: Encapsulate an array MyArray yourself, add some methods, and change to a dynamic variable-length array (the capacity of the array is not fixed. When the size == capicity is found, the capicity is doubled, and when size == capicity / 4, the capicity is reduced Twice. Size is the current number of elements in the array, and capacity is the maximum capacity of the array)

       head File:

#pragma once
#ifndef MYARRAY
#define MYARRAY

class MyArray
{
public:
	MyArray(int a);
	virtual ~MyArray();
	bool isEmpty();
	int getSize();
	int getCapicity();
	void addLast(int a);
	void addfirst(int a);
	void add(int pos, int a);//在pos索引位置添加元素
	int removeData(int pos);//删除pos位置元素,返回该位置被删的值
	int getData(int pos);
	void printData();
private:
	int *data;
	int m_isize;//数组当前元素个数
	int m_icapicity;//数组容量
	void resize(int newCapicity);//实现动态变长数组,当size超过容量的时候不报错,而是调用该函数,改变容量
};
#endif

       Source File:

#include "MyArray.h"
#include <iostream>
using namespace std;

MyArray::MyArray(int a)
{
	data = new int[a];
	m_icapicity = a;
	m_isize = 0;
}

MyArray::~MyArray()
{
	delete[]data;
	data = NULL;
}
bool MyArray::isEmpty() {
	return m_isize == 0;
}
int MyArray::getSize() {
	return m_isize;
}
int MyArray::getCapicity() {
	return m_icapicity;
}
void MyArray::addLast(int a) //在末尾添加元素
{
	if (m_isize == m_icapicity)
	{
		resize(m_icapicity *= 2);
	}
	data[m_isize++] = a;
}

void MyArray::printData() //打印数组
{
	for(int i = 0; i<m_isize;i++)
	{
		cout << data[i] << " ,";
	}
	cout << endl;
}

void MyArray::add(int pos, int a) //在pos索引位置添加元素
{
	if (m_isize == m_icapicity)
	{
		resize(m_icapicity *= 2);
	}
	if (pos > m_isize)
	{
		cout << "you input a wrong number which is out of the range" << endl;
	}
	else 
	{
		int i = m_isize;
		while (pos < i)
		{
			data[i] = data[i - 1];
			i--;
		}
		data[i] = a;
		m_isize++;
	}
}
void MyArray::addfirst(int a) //在起始处添加元素
{
	if (m_isize == m_icapicity)
	{
		resize(m_icapicity *= 2);
	}
	add(0, a);
}
int MyArray::removeData(int pos)//删除pos位置元素
{
	if (m_isize == m_icapicity / 4)//当删除元素使得size减小为capicity的1/4时,改变容量,减小1/2
	{
		resize(m_icapicity = m_icapicity / 2);
	}
	int ret = data[pos];
	for (int i = pos + 1; i < m_isize; i++) 
	{
		data[i-1] = data[i];
	}
	m_isize--;
	return ret;
}
int MyArray::getData(int pos) {
	if (pos>=m_isize)
	{
		printf("pos out of range!");
	}
	else
		return data[pos];
}

void MyArray::resize(int newCapicity)//实现动态变长数组,当size超过容量的时候不报错,而是调用该函数,改变容量
{
	m_icapicity = newCapicity;
	int *newData = new int[m_icapicity];
	for (int i = 0; i<m_isize; ++i)
	{
		newData[i] = data[i];
	}
	delete []data;
	data = newData;//先delete []data,然后让data指向新的空间.如果用java 这里不用delete,因为java有内存回收机制
}




Brief description of the idea: when adding elements, when size == capicity,

1 First create a new array space (newData), the capacity of this space is twice that of the original,

2 Then assign the value in the original array (data) to the new array (newData)

3 delete the original array space (delete [] data)

4 Let data point to the new space (data = newData)

When reducing the element, when the size is reduced to 1/4 or 1/2 of the capacity, the capacity is generally reduced. The steps are the same as above.

 

Note: It is not possible to modify only the capicity, because when creating an array, the space applied for is determined. To change the volume, you must reapply for a space.

Published 59 original articles · Likes46 · Visits 30,000+

Guess you like

Origin blog.csdn.net/sinat_41852207/article/details/88413228