我试图让我的vector/list/...中的每个指针都被删除,这个指针用超酷的lambda函数编写。lambda + for_each +在STL容器上删除
template <typename T>
void delete_clear(T const& cont)
{
for_each(T.begin(), T.end(), [](???){ ???->delete() });
}
我不知道该怎么填写???。任何帮助是极大的赞赏!
UPDATE:这是它应该是什么样子:
template <typename Container>
void delete_clear(Container &c)
{
for_each(c.begin(), c.end(), [](typename Container::value_type x){ delete x; });
c.clear();
}
你需要做'cont'非const为了调用'明确()'就可以了。并在'cont'上调用它,而不是T; -p – 2010-05-19 20:12:02
如果您需要指针容器,请查看Boost指针容器库(http://www.boost.org/doc/libs/1_43_0/libs/ptr_container/ DOC/ptr_container.html)。 – Philipp 2010-05-19 20:24:50
请勿参照容器。使用迭代器。 – pmr 2010-05-19 20:28:55