简体   繁体   English

新向量的向量

[英]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?我是否必须手动解除分配 map 中的每个向量,或者它们是由向量析构函数本身自动解除分配的? If I have to do it manually is this a good way to go:如果我必须手动完成,这是 go 的好方法吗:

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

?

You would need to free them with delete not delete [] because a vector is not an array.您需要使用delete而不是delete []来释放它们,因为向量不是数组。

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我是否必须手动解除分配 map 中的每个向量,还是自动解除分配

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.手动分配向量不是一个好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM