#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;
}
C++实现动态int数组
原创wx64015c4b4bc07 博主文章分类:C/C++/MFC ©著作权
文章标签 数组 #include ios 文章分类 JavaScript 前端开发
©著作权归作者所有:来自51CTO博客作者wx64015c4b4bc07的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:分治法求最大值
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++实现的动态数组
初学C++各种踩坑纪实
C/C++ 瞎写 -
【C++】动态数组
一、动态数组回顾计算机内存
c++ 动态数组 数组 编译器 -
c++实现动态数组存储学生信息
【代码】c++实现动态数组存储学生信息。
c++ 提示信息 数组 ci -
C语言的动态数组 VS C++的动态数组
C语言中的动态数组C语言中的动态数组
vector 动态数组 二维 i++ -
c、c++动态生成数组
C 二维数组动态分配和释放 (1)已知第二维 Code-1 char (*a)[N];
c++ 数组 休闲 c 动态 -
C++ 一维数组vector<int>
3、merge的时候指定vec3的位置一定要从begin开始,如果指定了end,它会认为没有空间。1、v
c++ 算法 数据结构 i++ 合并排序 -
c#接受c++动态数组指针
c#定义数组或向量传递指针参数,c++算法向数组指针输出参数数据
c++ c# 动态数组 -
C++数组实现栈
栈是一种先进后出的数据结构,元素只能从栈顶入栈,只能从栈顶出栈,如一组数入栈顺序为:、
c++ 算法 数据结构 入栈 出栈