我有以下动物对象。
Animal animalsArray = new Animal[maxSize];
int actualSize = 0;
我使用我的功能插入那里的对象 ->
void AnimalCatalog::Insert(const Animal& animal) {
if(actualSize <= maxSize) {
animalsArray[actualSize] = animal;
actualSize++;
}
}
我的问题是现在如何使用下面的函数从中删除任何对象?
void AnimalsCatalog::Delete(const char *animalName) {
int index = find(animalName);
< what to write here >
}
find() is my function which return index of the object from the
animalsArray
预先感谢!
您将从索引 1通过实际尺寸复制到实际尺寸索引,然后将实际尺寸降低1。
另外,以下测试将允许您的程序溢出数组界:
if(actualSize <= maxSize) {
进行测试&LT;而不是&lt; =。
使用 std::copy
:
std::copy(animalsArray + index + 1,
animalsArray + actualSize,
animalsArray + index);
--actualSize;