[cs13001] Slide error and Lab 12 question

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Apr 9 03:29:35 EDT 2013


>
> Also, I've struggling with my removeNumber function for the first
> part of lab 12.  Here's what my code looks like:
> 
> void removeNumber(vector<int> &v, int n)
> {
> for (vector<int>::iterator ip = v.begin(); ip < v.end(); ip++)
> if (*ip == n)
> v.erase(ip);
> 
> output(v);
> }
> 
> It's giving me an error saying that "Expression: vector iterator
> not incrementable  and I can't seem to figure out what the problem is.

erase() invalidates the iterator "ip" so you cannot use it
anymore. This includes incrementing it. Since you are removing a
single number you can use "break" after erase() to get out of the
loop.

Alternatively, erase() returns an iterator to the position after the
erased element, so something like this would keep your iterator valid:

   ip = v.erase(ip);


Thanks,
--
MIkhail


More information about the cs13001 mailing list