如何恢复已删除的数组元素?它是
template<class genType, int MAX_ITEMS >
void SortedList<genType, MAX_ITEMS>::DeleteItem(genType item) {
bool found = false;
int location = 0;
for (; location<length && !found; location++)
if (item == items[location])
found = true;
if (found) {
for (int index = location; index < length; index++)
items[index - 1] = items[index];
length--;
}
}
排序
答案 0 :(得分:0)
为了将元素恢复到数组,您只需要将所有已删除的元素存储在单独的数据结构中,并进行预删除。
为此,请在代码中添加以下内容:
if (found) {
deletedArray[dIter] = items[location];
dIter++;
for (int index = location; index < length; index++)
items[index - 1] = items[index];
length--;
}
现在,如果您想稍后将这些已删除的元素恢复到数组,您可以执行以下操作:
void restoreDeleted() {
for (int i = 0; i < dIter; i++)
insert(deletedArray[i]);
}