Remove an array element and shift the remaining ones

前端 未结 8 733
灰色年华
灰色年华 2020-11-29 01:09

How do I remove an element of an array and shift the remaining elements down. So, if I have an array,

array[]={1,2,3,4,5} 

and want to del

8条回答
  •  半阙折子戏
    2020-11-29 01:23

    You can't achieve what you want with arrays. Use vectors instead, and read about the std::remove algorithm. Something like:

    std::remove(array, array+5, 3)
    

    will work on your array, but it will not shorten it (why -- because it's impossible). With vectors, it'd be something like

    v.erase(std::remove(v.begin(), v.end(), 3), v.end())
    

提交回复
热议问题