用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,欢迎指出 !
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值