我正在执行一项任务,其中必须对Java样式ArrayList进行C ++实现。但是在几个方面,我遇到了麻烦。
.h和main.cpp的声明不能更改。
我有一个模板
template <class T>
class ArrayList
{
public:
// ********* Applying rule of 5: constructors ******************
// default constructor.
ArrayList() //OK
{
mElements = new T[5];
mReserved = 5;
mSize = 0;
}
// copy constructor
ArrayList(const ArrayList<T>& copyArray): mSize(copyArray.mSize),
mElements(new T[mSize])//OK
{
for (int i = 0; i < mSize; ++i)
mElements[i] = copyArray.mElements[i];
}
// move constructor
ArrayList(ArrayList<T>&& moveArray): mSize(moveArray.mSize),
mElements(moveArray.mElements)//OK
{
moveArray.mSize = 0;
moveArray.mElements = nullptr;
}
// copy assignment operator
ArrayList<T>& operator = (const ArrayList<T> ©ArrayOpt)//OK
{
if( ©ArrayOpt != this)
{
if (mSize != copyArrayOpt.mSize)
{
delete [] mElements;
mSize = copyArrayOpt.mSize;
mElements = new T[mSize];
}
for (int i = 0; i < mSize; ++i)
mElements[i] = copyArrayOpt.mElements[i];
return *this;
}
}
// move assignment operator
ArrayList<T>& operator = (ArrayList<T> && moveArrayOpt)//OK
{
if ( &moveArrayOpt != this)
{
delete [] mElements;
mSize = moveArrayOpt.mSize;
mElements = moveArrayOpt.mElements;
moveArrayOpt.mSize = 0;
moveArrayOpt.mElements = nullptr;
}
return *this;
}
virtual ~ArrayList()
{
delete [] mElements;
}
const T& operator[](int index) const;//OK
T& operator[](int index);//OK
int size() const;//OK
int reserved() const;//OK
bool isEmpty() const;//OK
void add(const T& element);//OK
void add(int index, const T& element);//OK
void remove(int index);//OK
void trimToSize();//OK
void mergeSort(ArrayList<T>& mElements, int low, int high);
void merge(ArrayList<T>& mElements, int left, int middle1, int middle2, int right);
ArrayList<T> subArrayList(int fromIndex, int toIndex) const;
T* toArray();
private:
void extendStorage();//OK
void wrongIndexInput();
int mSize, mReserved;
T* mElements;
};
引起麻烦的两个功能是
ArrayList<T> subArrayList(int fromIndex, int toIndex)
和
T* toArray()
。
这些我已经实现如下:
template <class T>
ArrayList<T> ArrayList<T>::subArrayList(int fromIndex, int toIndex) const
{
ArrayList<T> partialArray;
int sizeP = toIndex - fromIndex;
if (sizeP > partialArray.reserved())
{
partialArray.extendStorage();
}
for (int i = 0; i < (sizeP); ++i)
{
partialArray[i] = partialArray;
}
return partialArray;
}
和
template <class T>
T* ArrayList<T>::toArray()
{
std::vector<T*> array;
for (int i = 0; i < mSize; ++i)
{
array[i] = &mElements[i];
}
return *array.data();
}
在main.cpp中,两个函数的调用如下:
ArrayList<double> subArray = array.subArrayList(1,10);
std::cout << "Sub ArrayList have reserved space for " <<
subArray.reserved() << " elements and stores "
<< subArray.size() << " elements. " << std::endl;
for (int i = 0; i < subArray.size(); ++i)
{
std::cout << subArray[i] << ", ";
}
std::cout << "\b\b " << std::endl << std::endl;
// double* sArray = subArray.toArray();
for (int i = 0; i < 7; ++i)
{
subArray.add(i + 1);
}
std::cout << "Basic array of sub array contains: " <<
std::endl;
/*
for (int i = 0; i < subArray.size() - 7; ++i)
{
std::cout << sArray[i] << ", ";
}
std::cout << "\b\b " << std::endl << std::endl;
*/
std::cout << "Sub array with 7 elements added: " <<
std::endl;
for (int i = 0 ; i < subArray.size(); ++i)
{
std::cout << subArray[i] << ", ";
}
std::cout << "\b\b " << std::endl << std::endl;
在subArrayList的输出中说:
Sub ArrayList have reserved space for 10 elements and stores 0 elements.
我不知道为什么我不能在子数组中得到任何东西。
当我运行其他功能时,程序崩溃了。