由于迭代器在删除操作时无效,我如何用最少的代码迭代std::set的所有元素,删除其中的一些元素?
发布于 2012-01-06 05:47:46
// Filters the elements of a set: elements that satisfy the predicate 'pred'
// are removed from the source set and inserted into the output.
template <typename TSet, typename TOutputIterator, typename TPredicate>
void extract_if(TSet& s, TOutputIterator out, TPredicate pred)
{
for (typename TSet::iterator it(s.begin()); it != s.end();)
{
if (pred(*it))
{
*out++ = *it;
it = s.erase(it);
}
else
{
++it;
}
}
}https://stackoverflow.com/questions/8750231
复制相似问题