#include <iostream>
#include <string>
using namespace std;
class Array
{
private:
int size;
int length;
int* num;
public:
//初始化,数组长度为4
Array()
{
num=new int[4];
size=4;
length=0;
}
void add(int n)
{
if(length<size)
{
;
}
//每次空间满则加倍
else
{
int* temp=new int[size];
memcpy(temp,num,size);
delete[] num;
num=new int[size*2];
memcpy(num,temp,size);
delete[] temp;
size=size*2;
}
*(num+length)=n;
length++;
}
//取元素
int get(int n)
{
if(n<=length)
{
return *(num+n);
}
}
//取长度
int len()
{
return length;
}
//取容量
int con()
{
return size;
}
};
int main()
{
Array t;
t.add(5);
cout<<"长度:"<<t.len()<<endl;
cout<<"数组空间:"<<t.con()<<endl;
t.add(5);
cout<<"长度:"<<t.len()<<endl;
cout<<"数组空间:"<<t.con()<<endl;
t.add(5);
t.add(5);
cout<<"长度:"<<t.len()<<endl;
cout<<"数组空间:"<<t.con()<<endl;
t.add(5);
t.add(5);
cout<<"长度:"<<t.len()<<endl;
cout<<"数组空间:"<<t.con()<<endl;
return 0;
}