c++如何实现建立一个IVector类用来存放整型数组,,求帮忙编程制作。
整型相量IVector类的实现。建立一个IVector类用来存放整型数组,数组存放在动态分配的存储空间。该类拥有至少但不限于以下成员函数:构造函数:为对象分配动态存储空间...
整型相量IVector类的实现。建立一个IVector类用来存放整型数组,数组存放在动态分配的存储空间。该类拥有至少但不限于以下成员函数:构造函数:为对象分配动态存储空间,完成初始化,要考虑多种参数的重载形式,例如无参数、参数类型为int;析构函数:将占用的动态存储空间释放;拷贝构造函数:实现对象之间的深拷贝构造;at 函数:返回第i个元素,类似C数组,索引从0开始;front 函数:返回首元素;back 函数:返回尾元素;size 函数:返回数组的大小,即元素个数;sort 函数:将数组元素按值大小进行升排序;pop_back 函数:增加一个数据到末尾;push_back 函数:删除末尾的一个数据;重载了以下运算符:= 运算符:实现IVector对象之间的深拷贝赋值;+ 运算符:将两个IVector对象首尾相连;[] 运算符:访问IVector对象中某个下标位置的字符;<<运算符:输出IVector对象的所有元素;自行设计类中的数据成员和其它内容,并编写测试代码使用该IVector类。
展开
1个回答
展开全部
在要求基础上还扩展了几种比较常见的操作符和构造函数
#include <iostream>
#include <algorithm>
using namespace std;
class IVector
{
int *v;
int s;
public:
IVector();
IVector(int);
IVector(int *, int);
~IVector();
IVector(const IVector &);
int at(int) const;
int front()const;
int back()const;
int size()const;
void sort();
int pop_back(int);
int push_back();
IVector & operator = (IVector const & );
IVector &operator+=(IVector const &);
IVector operator+(IVector const & );
int operator[](int ) const;
int &operator[](int);
friend ostream & operator<<(ostream & , const IVector& );
};
IVector::IVector():v(NULL),s(0)
{
}
IVector::IVector(int value):s(1)
{
v = new int[1];
*v = value;
}
IVector::IVector(int *value, int number):v(NULL),s(0)
{
if(number <= 0 || value == NULL) return;
s = number;
v = new int [number];
int i;
for(i = 0; i < s; i ++)
v[i] = value[i];
}
IVector::~IVector()
{
if(v) delete []v;
}
IVector::IVector(const IVector &a):v(NULL),s(0)
{
if(a.s == 0 || a.v == NULL) return;
s = a.s;
v = new int [s];
int i;
for(i = 0; i < s; i ++)
v[i] = a[i];
}
int IVector::at(int i) const
{
return v[i];
}
int IVector::front()const
{
return v[0];
}
int IVector::back()const
{
return v[s - 1];
}
int IVector::size()const
{
return s;
}
void IVector::sort()
{
if(s <= 1) return ;
std::sort(v, v+s);
}
int IVector::pop_back(int value)
{
int *n(v);
if(n)
{
v = new int[s+1];
int i;
for(i = 0; i < s; i ++)
v[i] = n[i];
delete n;
}
v[s++] = value;
return s - 1;
}
int IVector::push_back()
{
s --;
return v[s];
}
IVector & IVector::operator = (IVector const & a)
{
if(v) delete [] v;
v = NULL;
s = 0;
if(a.s == 0 || a.v == NULL) return *this;
s = a.s;
v = new int [s];
int i;
for(i = 0; i < s; i ++)
v[i] = a[i];
return *this;
}
IVector &IVector::operator+=(IVector const &a)
{
if(a.s == 0 || a.v == NULL) return *this;
int *n = new int [s + a.s];
int i, j;
for(i = 0; i < s; i ++)
n[i] = v[i];
for(j = 0; j < a.s; j ++)
n[i+j] = a[j];
s+=a.s;
if(v) delete[]v;
v = n;
return *this;
}
IVector IVector::operator+(IVector const & a)
{
IVector t(*this);
t += a;
return t;
}
int IVector::operator[](int i) const
{
return v[i];
}
int &IVector::operator[](int i)
{
return v[i];
}
ostream & operator<<(ostream & os, const IVector& a)
{
if(a.v == NULL || a.s == 0)
os << "NULL";
else
{
int i;
for(i = 0; i < a.s; i ++)
{
if(i) os << ' ';
os << a[i] ;
}
}
return os;
}
int main()
{
IVector a;
cout << "no parameter IVector " << a << endl;
IVector b(1);
cout << "IVector(int) " << b << endl;
int arr[] = {6,5,7,1,6,3,4};
IVector c(arr, 7);
cout << "IVector(int *, int)" << c << endl;
IVector d(c);
cout << "IVector(const IVector &);" << d << endl;
cout << "d.at[3] = " << d.at(3) << endl;
cout << "d.front = " << d.front()<< endl;
cout << "d.back = " << d.back()<< endl;
cout << "d.size = " << d.size()<< endl;
d.sort();
cout << "after sort " << d << endl;
cout << "test pop_back and push_back\nlooks like name wrong in these two functions\n";
cout << "pop back = " << d.pop_back(10) << endl;
cout << "after pop back " << d << endl;
cout << "push back = " << d.push_back() << endl;
cout << "after push back " << d << endl;
IVector e;
e = b;
cout << "test operator = :\n" << e << " = " << b << endl;
cout << e << "+=" << d << ":\n";
e += d;
cout << e << endl;
cout << b << " + " << c << " = " << b + c << endl;
cout << c<< '[' << 3<<']' << '=' << c[3] << endl;
cout << "c=" << c << endl << "c[2] = 100, then c = ";
c[2] = 100;
cout << c << endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询