首页 > 代码库 > 删除 vector / list 中所有指针元素

删除 vector / list 中所有指针元素

高级写法:

 std::list<T*> list_pts; while (!list_pts.empty()) {   delete list_pts.front(),list_pts.pop_front(); }  std::vector<T*> vector_pts; while (!vector_pts.empty()) {   delete vector_pts.back(),vector_pts.pop_back(); } //wrap into a function:template<typename Container>void DeleteAllPointers (Container& container) {  while (!container.empty()) {    delete container.back(),container.pop_back();  }}

 

通俗写法:

std::list<T*> list_pts;for (list<T*>::const_iterator itr=list_pts.begin(); itr!=list_pts.end(); ++itr) {  delete *itr;}list_pts.clear();

删除 vector / list 中所有指针元素