Search code examples
c++vectorstructiteratorerase

c++: vector::erase called with an iterator


I am working on a vector of structs.

When I am trying to call this function with iterator, like this:

vec2.erase (vec2.begin()+iter2);

it sends me this error:

"no match for 'operator+' in '(+vec2)->std::vector<_Tp, _Alloc>::begin [with _Tp = wordstype, _Alloc = std::allocator<wordstype>]() + iter2'" 

Can I send an iterator (or any other parameter)? If yes, what is the problem?


Solution

  • You cannot add iterator to iterator.

    iter2 already points to the concrete place in a vector, so you can write:

    iter2 = vec2.erase(iter2);
    

    instead.