2014金山笔试_编写一个数组类 MyVector

时间:2021-12-03 08:32:48
 //编写一个数组类 MyVector,数组内容可以动态扩充,实现构造,析构,赋值操作符重载,插入,删除,获取元素个数,获取数组容量(不可以使用STL等的容器类,不能使用
//不连续的存储空间)
#include<iostream>
using namespace std;
class MyVector
{
public:
MyVector()
{
size=;
val=new int[count];
iterator=val;
start=val;
end=val;
Counts=count;
}
MyVector(int N)
{
size=;
N=N/count+;
val=new int[count*N];
iterator=val;
start=val;
end=val;
Counts=count*N;
}
~MyVector()
{
delete[] val;
}
MyVector & operator=(const MyVector & vec)
{
delete[] val;
val=vec->val;
size=vec->size;
Counts=vec->Counts;
start=vec->start;
end=vec->end;
}
void Insert(int num)
{
size++;
if(size>Counts)
{
Counts=Counts+count;
newval=new int[Counts];
for(int i=;i<size-;i++)
{
newval[i]=val[i];
}
delete(val);
val=newval;
start=val;
end=val+size-;
}
*end=num;
end++;
}
int Delete()
{
size--;
end--;
return *end;
}
int Size()
{
return size;
}
int Count()
{
return Counts;
}
int *val;
int *start;
int *end;
private:
static const int count=;
int Counts;
int *iterator;
int size;
int *newval;
}; int main()
{
MyVector* vec=new MyVector();// MyVector* vec=new MyVector(15);
MyVector* vec2=new MyVector;
int k;
for(int i=;i<;i++)
{
vec->Insert(i);
}
cout<<"插入前:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count()<<endl; vec->Insert();
cout<<"插入后:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<"数量"<<vec->Size();
cout<<"容量"<<vec->Count()<<endl; for(int i=;i<;i+=)
{
vec->Insert(i);
}
cout<<"插入后:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count()<<endl;
k=vec->Delete();
cout<<"删除一个后:";
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count();
cout<<" ,删除的元素为:"<<k<<endl; vec2=vec;
cout<<"给别个赋值后:";
for(int i=;i<vec2->Size();i++)
{
cout<<vec2->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec2->start;
cout<<" ,结束值"<<*(vec2->end-);
cout<<"数量"<<vec2->Size();
cout<<"容量"<<vec2->Count();
cout<<endl; system("pause");
return ;
}

2014金山笔试_编写一个数组类 MyVector

采用模板类编程为:

 //编写一个数组类 MyVector,数组内容可以动态扩充,实现构造,析构,赋值操作符重载,插入,删除,获取元素个数,获取数组容量(不可以使用STL等的容器类,不能使用
//不连续的存储空间)
#include<iostream>
using namespace std;
template<class T>
class MyVector
{
public:
MyVector()
{
size=;
val=new T[count];
iterator=val;
start=val;
end=val;
Counts=count;
}
MyVector(int N)
{
size=;
N=N/count+;
val=new int[count*N];
iterator=val;
start=val;
end=val;
Counts=count*N;
}
~MyVector()
{
delete[] val;
}
MyVector & operator=(const MyVector & vec)
{
delete[] val;
       val* = new T[vec.size()]; size=vec->size;
Counts=vec->Counts;
start=val;
end=val+vec.size();
}
void Insert(int num)
{
size++;
if(size>Counts)
{
Counts=Counts+count;
newval=new T[Counts];
for(int i=;i<size-;i++)
{
newval[i]=val[i];
}
delete(val);
val=newval;
start=val;
end=val+size-;
}
*end=num;
end++;
}
int Delete()
{
size--;
end--;
return *end;
}
int Size()
{
return size;
}
int Count()
{
return Counts;
}
T *val;
T *start;
T *end;
private:
static const int count=;
int Counts;
T *iterator;
int size;
T *newval;
}; int main()
{
MyVector<char>* vec=new MyVector<char>();// MyVector* vec=new MyVector(15);
MyVector<char>* vec2=new MyVector<char>;
char k;
for(int i=;i<;i++)
{
vec->Insert(i+);
}
cout<<"插入前:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count()<<endl; vec->Insert('-');
cout<<"插入后:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<"数量"<<vec->Size();
cout<<"容量"<<vec->Count()<<endl; for(int i=;i<;i++)
{
vec->Insert(i+);
}
cout<<"插入后:";
for(int i=;i<vec->Size();i++)
{
cout<<vec->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count()<<endl;
k=vec->Delete();
cout<<"删除一个后:";
cout<<"起始值"<<*vec->start;
cout<<" ,结束值"<<*(vec->end-);
cout<<" ,数量"<<vec->Size();
cout<<" ,容量"<<vec->Count();
cout<<" ,删除的元素为:"<<k<<endl; vec2=vec;
cout<<"给别个赋值后:";
for(int i=;i<vec2->Size();i++)
{
cout<<vec2->val[i]<<" ";
}
cout<<endl;
cout<<"起始值"<<*vec2->start;
cout<<" ,结束值"<<*(vec2->end-);
cout<<"数量"<<vec2->Size();
cout<<"容量"<<vec2->Count();
cout<<endl; system("pause");
return ;
}

2014金山笔试_编写一个数组类 MyVector

val