C dynamically growing array

前端 未结 7 2116
闹比i
闹比i 2020-11-22 07:57

I have a program that reads a \"raw\" list of in-game entities, and I intend to make an array holding an index number (int) of an indeterminate number of entities, for proce

7条回答
  •  天涯浪人
    2020-11-22 08:18

    Well, I guess if you need to remove an element you will make a copy of the array despising the element to be excluded.

    // inserting some items
    void* element_2_remove = getElement2BRemove();
    
    for (int i = 0; i < vector->size; i++){
           if(vector[i]!=element_2_remove) copy2TempVector(vector[i]);
           }
    
    free(vector->items);
    free(vector);
    fillFromTempVector(vector);
    //
    

    Assume that getElement2BRemove(), copy2TempVector( void* ...) and fillFromTempVector(...) are auxiliary methods to handle the temp vector.

提交回复
热议问题