C++从数组中删除和取消删除元素

C++从数组中删除和取消删除元素,c++,arrays,arraylist,sortedlist,C++,Arrays,Arraylist,Sortedlist,如何恢复已删除的数组元素?这是一个 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++) i

如何恢复已删除的数组元素?这是一个

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--;
    }
}

排序

为了将元素恢复到数组中,只需将所有删除的元素存储在单独的数据结构中,即删除前

为此,请在代码中添加以下内容:

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]);
}

编辑以进一步澄清。