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;
?
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
Do i have to deallocate each of the vectors inside map manually or are they deallocated automatically
If you allocate manually, then you must deallocate manually (unless you transfer ownership to a smart pointer in which case you preferably should not have allocated manually in the first place).
Allocating vectors manually is not a good way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.