[Solved] C++ How to remove 0 values from array without using vector


I still prefer using std::vector although this question mentions “without using vector”. But let’s just try doing so with array anyway.

int int_array[20] = {/*...*/};

int* last_ptr = std::remove(std::begin(int_array), std::end(int_array), 0);

for (int* it = int_array ; it != last_ptr ; ++it)
    cout << *it << endl;

As convention, the resulting last_ptr points to the location one pass the end of the resulting array.

Since it is an array, the actual array size does not change. All we can do is to ignore the unused part of the array.

2

solved C++ How to remove 0 values from array without using vector