用c++实现一个简易的vector

using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::endl;


class VectorDouble
{
public:
	bool operator =(const VectorDouble&); 
	friend bool operator ==(const VectorDouble&,const VectorDouble&);

	int capasity();
	int size();
	bool push_back(double);
	bool pop_back();

	bool reserve(int);
	bool resize(int);
	bool empty();
	double value_at(int);
	void change_value_at(int,double);
	void traval();

	VectorDouble();
	VectorDouble(int);
	VectorDouble(VectorDouble&);
	~VectorDouble();



private:
	bool full();
	double *p;
	int count;
	int max_count;
};
void VectorDouble::traval()
{
	for(int i=0;i<count;i++)
	{
		cout<<p[i]<<" ";
	}
	cout<<endl;
}
bool VectorDouble::operator =(const VectorDouble& temp)
{
	if(max_count>=temp.count)
	{
		count=temp.count;
		max_count=temp.max_count;
		p=new double[max_count];
		for(int i=0;i<temp.count;i++)
			p[i]=temp.p[i];
	}
	else
	{
		delete [] p;
		p=new double[temp.count];
		for(int i=0;i<temp.count;i++)
			p[i]=temp.p[i];
	}
	return true;
}
bool operator ==(const VectorDouble& data1,const VectorDouble& data2)
{
	bool flag=true;
	if(data1.count!=data2.count && flag==true)
		flag=false;
	if(flag==true)
	{
		for(int i=0;i<data1.count;i++)
		{
			if(data1.p[i]!=data2.p[i])
			{
				flag=false;
				break;
			}
		}
	}
	return flag;
}
/*******************************************/
VectorDouble::VectorDouble()
{
	p=new double[50];
	count=0;
	max_count=50;
}
VectorDouble::VectorDouble(int length)
{
	p=new double[length];
	count=0;
	max_count=length;
}
VectorDouble::VectorDouble(VectorDouble& temp)
{
	count=temp.count;
	max_count=temp.max_count;
	p=new double[max_count];
	for(int i=0;i<temp.count;i++)
		p[i]=temp.p[i];
}
/*******************************************/
VectorDouble::~VectorDouble()
{
	delete [] p;
}
/*******************************************/

bool VectorDouble::full()
{
	if(count==max_count)
		return true;
	else
		return false;
}
bool VectorDouble::empty()
{
	if(count==0)
		return true;
	else
		return false;
}

/*******************************************/
int VectorDouble::capasity()
{
	return max_count;
}
int VectorDouble::size()
{
	return count;
}
/*******************************************/
bool VectorDouble::reserve(int length)
{
	if(length<=max_count)
		return false;

	double *temp;
	temp=new double[max_count];
	for(int i=0;i<count;i++)
	{
		temp[i]=p[i];
	}
	delete [] p;

	max_count=length;
	p=new double[max_count];
	for(i=0;i<count;i++)
	{
		p[i]=temp[i];
	}
	delete [] temp;

	return true;
}

bool VectorDouble::resize(int length)
{
	if(length<=max_count)
	{
		count=length;
	}
	else if(length>max_count)
	{
		count=length;
		reserve(length);
	}
	return true;
}

bool VectorDouble::push_back(double data)
{

	if(full())
	{
		reserve(max_count*2);
		p[count++]=data;
	}
	else
	{
		p[count++]=data;
	}

	return true;
}
bool VectorDouble::pop_back()
{
	if(empty())
	{
		cout<<"VectorDouble Empty"<<endl;
		exit(1);
	}
	else
	{
		count--;
	}
	return true;
}


/*******************************************/
double VectorDouble::value_at(int i)
{
	if(i>=count)
	{
		cout<<"Error location"<<endl;
		return -1;
	}
	return p[i];
}
void VectorDouble::change_value_at(int i,double data)
{
	if(i>=count)
	{
		cout<<"Error location"<<endl;
	}
	p[i]=data;
}
/*******************************************/




由于是一个简易的vector,所以在写的时候只假定存放double类型的值,可以写成一个模板类,效果更好哟,亲

在写这个的时候,发现vc6.0的一个惊天大bug,不能在友元函数中访问private变量,估计是命名空间的问题。

不用 

using namespace std;

换成每一个小项

using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::endl;

编译通过,测试正常

可能存在一些bug,欢迎指出 !
















好的。 首先,需要了解什么是 vectorVector 是一种动态数组,它可以动态地增加或减少元素。 下面是使用 C 语言实现 vector 的一种方式: ``` #include <stdio.h> #include <stdlib.h> typedef struct { int *data; // 指向动态数组的指针 int size; // 动态数组的大小 int capacity; // 动态数组的容量 } Vector; // 初始化 vector void vector_init(Vector *vector) { vector->data = malloc(sizeof(int) * 4); vector->size = 0; vector->capacity = 4; } // 销毁 vector void vector_destroy(Vector *vector) { free(vector->data); } // 在 vector 末尾添加元素 void vector_push_back(Vector *vector, int value) { if (vector->size == vector->capacity) { // 动态数组已满,需要扩容 vector->capacity *= 2; vector->data = realloc(vector->data, sizeof(int) * vector->capacity); } vector->data[vector->size++] = value; } // 在 vector 中删除最后一个元素 void vector_pop_back(Vector *vector) { if (vector->size > 0) { vector->size--; } } // 获取 vector 中指定位置的元素 int vector_get(Vector *vector, int index) { if (index >= 0 && index < vector->size) { return vector->data[index]; } return 0; } // 在 vector 中的指定位置插入元素 void vector_insert(Vector *vector, int index, int value) { if (index < 0 || index > vector->size) { return; } if (vector->size == vector->capacity) { vector->capacity *= 2; vector->data = realloc(vector->data, sizeof(int) * vector->capacity); } for (int i = vector->size; i > index; i--) { vector->data[i] = vector->data[i - 1]; } vector->data[index] = value; vector->size++; } // 在 vector 中删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值