📄 vector.h
字号:
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream.h>
#include <stdlib.h>
//没有用上
template <class T>
class Vector
{
T *ptr;
int arraySize;
void memError(void);
void subError(void);
public:
Vector();
Vector(int);
Vector(const Vector &);
~Vector();
int size();
void operator =(const Vector &);
void Plus(const char goal[]);
bool Minus(const char goal[]);
T &operator [](const int &);
};
template <class T>
Vector<T>::Vector()
{
ptr = NULL;
arraySize = 0;
}
template <class T>
Vector<T>::~Vector()
{
if(arraySize > 0)
delete [] ptr;
}
template <class T>
Vector<T>::Vector(int SIZE)
{
arraySize = SIZE;
ptr = new T[SIZE];
if(SIZE == 0)
memError();
for(int count=0;count < arraySize;count++)
*(ptr + count) = '\0'; //因为本项目中仅用了字符串组,
} //故可用strcpy();以后有需要还可进行重载
template <class T>
Vector<T>::Vector(const Vector &goal)
{
arraySize = goal.arraySize;
ptr = new T[arraySize];
if(ptr ==0)
memError();
for(int count=0;count<arraySize;count++)
strcpy(*(ptr + count) , *(goal.ptr + count)); //因为本项目中仅用了字符串组,
} //故可用strcpy();以后有需要还可进行重载
template <class T>
int Vector<T>::size()
{
return arraySize;
}
template <class T>
void Vector<T>::operator =(const Vector &goal)
{
arraySize = goal.arraySize;
ptr = new T[arraySize];
if(ptr ==0)
memError();
for(int count=0;count<arraySize;count++)
*(ptr + count) = *(goal.ptr + count);
}
template <class T>
void Vector<T>::Plus(const char goal[])
{
Vector temp(arraySize+14);
for(int count=0;count < arraySize;count++)
*(temp.ptr + count) = *(ptr + count);
for(int i = 0;count < arraySize+14;count++)
*(temp.ptr + count) = *(ptr + i);
*this = temp;
}
template <class T>
bool Vector<T>::Minus(const char goal[])
{
bool sign = false;
Vector temp(arraySize-14);
int b=0;
for(int i=0,j=0;j < arraySize;)
{
if(*(temp.ptr + b*14) = *(ptr + 0))
{
for(int k=1;k<14;k++)
if(*(temp.ptr + b*14+k) = *(ptr + k))
continue;
else break;
if(k = 14)
{
sign = true;
break;
}
}
else
b++;
}
int k;
for(i=b*14,k=(b+1)*14;k<arraySize;i++,k++)
*(temp.ptr + i) = *(ptr + k);
*this = temp;
return sign;
}
template <class T>
T & Vector<T>::operator [](const int &sub)
{
if(sub<0||sub>arraySize)
subError();
return ptr[sub];
}
template <class T>
void Vector<T>::memError(void)
{
cout<<"动态数组申请内存时出错!"<<endl;
exit(0);
}
template <class T>
void Vector<T>::subError(void)
{
cout<<"动态数组下标越界!"<<endl;
exit(0);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -