开发者

c++ vector of vectors destruction

开发者 https://www.devze.com 2023-01-26 06:22 出处:网络
hi I\'ve vector<vector<int> > matrix; I initialize it by: inline void resize(const UINT nrows, const UINT ncols, T val) {

hi I've vector<vector<int> > matrix; I initialize it by:

inline void resize(const UINT nrows, const UINT ncols, T val) {
vector<T> v_rows(ncols, val);
matrix.resize(nrows, v_rows);
}

now I'm concerned if I call resize again where the old ones go, do I have to call clear ? for the outer matrix ? or clear for each one matrix.at(i).cl开发者_运维百科ear(); or I don't need to do anything ?


You don't have to do anything as long a T manages it's own resources. Which, if it doesn't, clear() isn't going to help.


If you call clear (or a resize with smaller size) on a vector of anything, then all elements from that vector which need to be deleted have their destructors called and their memory is released.

If you have a vector of vectors, then each inner vector's destructor will clean up its resources properly. When a row-vector or column-vector is destroyed, it cleans up after itself automatically.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号