【发布时间】:2016-04-15 13:28:47
【问题描述】:
我有一个情况。我为我的一项任务使用了模板化函数。对于这个函数,我通过引用传递迭代器。现在,我必须从向量中删除几个元素。我如何只使用迭代器来做到这一点?请找到相应的代码:
template <class BidirectionalIterator, class Iterator> bool
SomeFunc( BidirectionalIterator& first, BidirectionalIterator& last, Iterator anotherVecBegin )
{
while((first+1) != last)
{
if(some_condition)
// delete (first); HOW?
else if(some_other_condition)
// delete (first + 1); HOW?
}
// add something to another vector using anotherVecBegin
return true;
}
有很多已经被问到的问题,但它们都有一个上下文向量。所以myVec.erase(*first) 很简单..
我也知道通过引用传递迭代器并不是一个很好的方法。但我遵循简单的规则:当某些东西需要改变或避免大量复制时使用引用。我的场景符合第一个条件。
那么我该如何删除呢?
【问题讨论】:
标签: c++ templates c++11 vector iterator