1_c++实现简单vector

C++模板类实现动态数组操作
本文介绍了一个使用C++模板实现的动态数组类,包括增加、删除、判断空、修改、尾部插入等功能,并提供了详细的注释。通过简单的测试文件展示了类的功能和用法。

实现了一些大概的功能,增加,删除,判空,修改,尾部插入,注释很详细,直接贴代码

头文件:CustomVector.h

#pragma once
template<class T,int n>
class CustomVector
{
private:
 T *p;//初始化数组p
 int cur_len;//记录现在有效长度的元素个数
 int len;//记录总长度
public:
	CustomVector<T,n>(void);
	~CustomVector<T,n>(void);
	void addNodeToLast(int val);
	void showVector();
	void deleNode(int pos);
	void insert(int pos,int value);
	void update(int pos,int value);
	bool isEmpty();
};

类文件:CustomVector.cpp

#include "CustomVector.h"

template<class T,int n>
CustomVector<T,n>::CustomVector(void)
{
	this->p=new T[n];
	this->len=n;
	this->cur_len=len;
	for (int i=0;i<n;i++)
	{
		*(p+i)=i;
	}

}

template<class T,int n>
CustomVector<T,n>::~CustomVector(void)
{
	delete [] p;
}
template<class T,int n>
void CustomVector<T,n>::addNodeToLast(int val){
	if (this->cur_len<this->len)//当数组长度还没有满的时候
	{
		*(p+(this->cur_len++))=val;
	}else{
		//重新申请一片内存,进行数组拷贝
		T *pt=p;
		p=new T[++this->len];
		for (int i=0;i<this->cur_len;i++)
		{
			*(p+i)=*(pt+i);
		}
		p[this->cur_len]=val;
		this->cur_len++;
	}
}
template<class T,int n>
void CustomVector<T,n>::showVector(){
	for (int i=0;i<this->cur_len;i++)
	{
		printf("%d\t",*(p+i));
	}
	printf("\n");
}
template<class T,int n>
void CustomVector<T,n>::deleNode(int pos){
	if (pos<0||pos>this->cur_len)
	{
		return ;
	}
	//把后面的元素往前挪动一个单位,然后长度-1
	for (int i=pos-1;i<cur_len;i++)
	{
		p[i]=p[i+1];
	}
	cur_len--;

};
template<class T,int n>
void CustomVector<T,n>:: insert(int pos,int value){
	if (pos<0||pos>cur_len)
	{
		return ;
	}
	//申请一片+1的内存

	if (cur_len>=len)
	{

		T *pt=p;
		p=new T[++len];
		//拷贝数据
		for (int i=0;i<cur_len;i++)
		{
			*(p+i)=*(pt+i);
		}
	}else{
		//往后挪动一个位置
		for (int i=cur_len-1;i>=pos-1;i--)
		{
			p[i+1]=p[i];//把pos-1之后的元素都往后挪动一个单位
		}
		p[pos-1]=value;
		cur_len++;//插入成功后+1
	}

}
template<class T,int n>
bool CustomVector<T,n>:: isEmpty(){
	if (cur_len==0)
	{
		return false;
	}else{
		return true;
	}
}
template<class T,int n>
void CustomVector<T,n>::update(int pos,int value){
	if (pos<0||pos>len)
	{
		return ;
	}
	p[pos-1]=value;
}

简单测试文件:main.cpp

#include <stdlib.h>
#include <stdio.h>
using namespace std;
#include "CustomVector.h"
#include "CustomVector.cpp"
void main(){
	CustomVector<int ,10> vctor;
	printf("初始化数据:\n");
	vctor.showVector();
	vctor.addNodeToLast(5);
	printf("插入一个节点:\n");
	vctor.showVector();
	printf("删除两个节点:\n");
	vctor.deleNode(0);
	vctor.deleNode(1);
	vctor.showVector();
	printf("插入一个节点\n");
	vctor.insert(5,3);
	vctor.showVector();
	printf("修改一个节点\n");
	vctor.update(5,4);
	vctor.showVector();
	system("pause");
}


输出:

初始化数据:
0       1       2       3       4       5       6       7       8       9


插入一个节点:
0       1       2       3       4       5       6       7       8       9
5
删除两个节点:
2       3       4       5       6       7       8       9       5
插入一个节点
2       3       4       5       3       6       7       8       9       5

修改一个节点
2       3       4       5       4       6       7       8       9       5


Press any key to continue . . .


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值