I have a vector full of words and I am trying to erase a chunk of that vector at a specified beginning and end. For example:
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = { "The", "Quick", "Brown", "Fox", "Jumps", "Over", "The", "Lazy", "Dog" };
words.remove_chunk(1, 2);
}
Here, words.remove_chunk(1, 2);
would erase the items at index 1 through 2, leaving the vector to be:
{ "The", "Fox", "Jumps", "Over", "The", "Lazy", "Dog" }
How would I go about efficiently writing remove_chunk
? Is there an stl function for this or a quick one-liner?
Use vector::erase
:
words.erase(words.begin(), words.begin() + 2);
Note: Second iterator provided is words.begin() + 2
instead of words.begin() + 1
because STL uses [begin, end).