我正在为一个动态的字符串数组创建一个容器类。我知道使用 std::vector 会更好,但这不是分配的“指针”。
据我所知,我的程序在下面的 delete [] lineArray 步骤中挂起。
MyBag::MyBag()
{
nLines = 0;
lineArray = new std::string[0] ();
}
void MyBag::ResizeArray(int newLength)
{
std::string *newArray = new std::string[newLength];
//create new array with new length
for (int nIndex=0; nIndex < nLines; nIndex++)
{
newArray[nIndex] = lineArray[nIndex];
//copy the old array into the new array
}
delete[] lineArray; //delete the old array
lineArray = newArray; //point the old array to the new array
nLines = newLength; //set new array size
}
void MyBag::add(std::string line)
{
ResizeArray(nLines+1); //add one to the array size
lineArray[nLines] = line; //add the new line to the now extended array
nLines++;
}
整个程序在这里http://pastebin.com/KnL4XmAw