简体   繁体   中英

Deleting elements in a vector of pointers to vectors of objects

I have a vector set up as follows:

vector< vector<myClass>* > vec;

I am having trouble deleting elements from vec however, I have dynamically allocated all elements in vec, and am trying to delete the ith element as follows:

vector<myClass> *victim = vec[i];
delete victim;
victim = 0;

However this does not seem to be correctly removing them from vec. What am I missing here?

try using vector::erase to remove it from the vector. The delete will free the memory only but the vector will keep the pointer unless you removed it.

have a look at this answer https://stackoverflow.com/a/3385251/249120

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.

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