Delete element from C++ array

前端 未结 11 870
逝去的感伤
逝去的感伤 2020-12-11 14:26

Please tell me how to delete an element from a C++ array.

My teacher is setting its value to 0, is that correct?

11条回答
  •  情歌与酒
    2020-12-11 15:08

    it's an old topic but I'm gonna put this answer here for anyone who's recently seen this question!

    deleting an element from the array is a heavy operation you can easily keep track of your elements with an index. anyway, you can call this function whenever you want to delete the first x element of a vector.

    vector remove_frist_x_items(const vector& arr, int x)
    {
        return vector(arr.begin() + x, arr.end());
    }
    
    // Delete the first element from the array
    auto new_array = remove_frist_x_items(the_old_one, 1);
    

提交回复
热议问题