+ 1
Eliminar elementos de un vector en C
Requiero de eliminar elementos de un vector, espero me puedan ayudar.
2 Réponses
+ 1
You may have to translate this from English to Spanish to understand.
To remove an element from a vector, you need to pass in an index of the element you want to remove. (Do some bounds checking on that index so it's not larger than the element count).
Then access the vector's internal array at position 'index' and set it to NULL.
If you have heap-allocated data that needs to be freed, you could have the vector return the removed element by storing it in a temp variable. But I'll leave that up to you.
Now that you have an empty slot, you have to shift all the elements in front of that empty slot back one step. To do that use a loop which starts from position 'index' and iterates until it reaches the vector's element count - 1. (The -1 part is important because we'll be accessing the element one step ahead).
In the body of the loop assign the next vector element to the current slot in the iteration, and set the next slot to NULL. That's it for the removal.
The last and final step is to decrease the element count by one (since you removed an entry) and then check if it's possible for the vector capacity to shrink before returning.
0
Thank so much for your help!.