Deallocating objects stored in a vector?

前端 未结 7 494
一生所求
一生所求 2020-12-10 06:17

I have a class that creates a vector of objects. In the deconstructor for this class I\'m trying to deallocate the memory assigned to the objects. I\'m trying to do this by

7条回答
  •  不知归路
    2020-12-10 06:43

    If you're using std::vector then you can just let it get handled by the destructor for vector, assuming there are "objects" (and not pointers to objects) in said vector.

    -- or --

    If you're using a standard array as the "vector":

    The purpose of the "delete []" variation is to deallocate an entire array, and hence avoid the need to have a for loop like you do.

    If using standard C/C++ arrays, "delete [] maps" should do it for you. "[]" shouldn't be used for deallocating STL vectors.

提交回复
热议问题