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

本文介绍了一个简易版Vector类的设计与实现,主要支持double类型的数据存储。文章详细展示了类的方法定义及其实现逻辑,包括构造与析构函数、赋值运算符重载、比较运算符重载等,并讨论了VC6.0环境下的一些编译问题。
2105

被折叠的 条评论
为什么被折叠?



