I have a situation. I used a template function for one of my tasks. To this function, I pass an iterator by reference. Now I have to remove some elements from the vector. How to do this using only iterators? Pl find the appropriate code:
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; }
There are many questions already asked, but they all have a vector in context. therefore myVec.erase(*first) easy ..
I also know that this is not a very good way to pass an iterator by reference. But I follow simple rules: use links when something needs to change or to avoid a heavy copy. My script meets the first condition.
So how to remove?
source share