在练习13.43中,推荐使用for_each和lambda替代原来的for循环destory元素。贴出代码
for (auto i = first_free; i != elements; i++) {
alloc.destroy(--i);
}
for_each(elements, first_free, [this](std::string &rhs) { alloc.destroy(&rhs); });
第二种使用for_each进行循环时,不需要考虑传入指针的顺序问题,不用考虑什么时候停止,只需要向其传入一段范围即可。