Search code examples
c++vectordynamic-memory-allocation

Vector of new vectors


Suppose I have a vector of dynamically allocated vectors, something like:

std::vector<std::vector<double>*> map;

Do i have to deallocate each of the vectors inside map manually or are they deallocated automatically by the vector destructor itself? If I have to do it manually is this a good way to go:

for(auto& t : map) delete[] t;

?


Solution

  • You would need to free them with delete not delete [] because a vector is not an array.

    But I wouldn't see any reason why you wouldn't use

    std::vector<std::vector<double>>

    This way you wouldn't need to worry about the allocation of the vector