I have a dynamically created array of integers. Now I have to remove all elements which have index %3 == 0. (for example, 3, 6, 9, …). So, what is the best way to decrease array size? With malloc I can use realloc for the same part of memory, but what about new operator? What to do this way. Just slide all elements left, make zero to all another elements?
I have a dynamically created array of integers. Now I have to remove all
Share
I’d simply allocate a new smaller array and then copy elements to it. Something like this (this includes the element at 0 index):
You can of course work in place and shift elements to the left. But you can’t delete a part of array that was allocated via
new[].Since this is an exercise, and you can’t use STL, why don’t you try to implement a simple vector class yourself?