Can I call `delete` on a vector of pointers in C++ via for_each <algorithm>?
问题 Suppose I have a std::vector<Obj *> objs (for performance reasons I have pointers not actual Obj s). I populate it with obj.push_back(new Obj(...)); repeatedly. After I am done, I have to delete the pushed-back elements. One way is to do this: for (std::vector<Obj *>::iterator it = objs.begin(); it != objs.end(); ++it) { delete *it; } However, I am interested if I can use for_each algorithm to do the same: #include <algorithm> ... for_each(objs.begin(), objs.end(), delete); What do you think?