I'm trying to splice a vector in C++ using the following code:
sequence(sequence.begin() + i, sequence.end());
where sequence
is a vector containing integers and i
is an integer, but when I run my code it gives the following error:
error: no match for call to ‘(std::vector<int>) (__gnu_cxx::__normal_iterator<int*,
std::vector<int> >, std::vector<int>::iterator)’
sequence(sequence.begin() + i, sequence.end());
^
I don't understand what is wrong with my code. I used the following stack overflow questions to help write this code:
You can't do that, because sequence
doesn't have an overloaded call operator. You can only use that syntax when initializing objects when you are defining them:
int i(1); // ok
i(2); // nope!
You are trying to do the second line above, which fails because it's not a valid form of initialization in this context. If you want to splice a vector, you can use std::vector::erase
;
sequence.erase(sequence.begin() + i, sequence.end()); // erases [start, end)