Programming Tips - How can I delete an element from an array in C/C++ ?
Q. How can I delete an element from an array in C/C++ ?
A. Here is an example of a function that can do it:
inline void DeleteArrayElement(THING a, const int i, const int n)
memmove(&a[i], &a[i+1], (n - i - 1) * sizeof(THING));
THING my_things; // Declare the array
// put some stuff into the array
// Delete element 5
DeleteArrayElement(a, 5, sizeof(my_things) / sizeof(my_things));
Use erase() to delete an element from an STL vector (which is a growable array).