从 Vector 中删除一大块项目?

Remove a Chunk of Items from a Vector?

我有一个充满单词的向量,我正试图在指定的开始和结束处擦除该向量的一部分。例如:

#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = { "The", "Quick", "Brown", "Fox", "Jumps", "Over", "The", "Lazy", "Dog" };
    remove_chunk(words, 1, 2);
}

此处,remove_chunk(words, 1, 2); 将删除索引 1 到 2 处的项目,使向量为:

{ "The", "Fox", "Jumps", "Over", "The", "Lazy", "Dog" }

我将如何高效地编写 remove_chunk?是否有用于此或快速单行的 stl 函数?

使用vector::erase:

words.erase(words.begin(), words.begin() + 2);

注意:提供的第二个迭代器是 words.begin() + 2 而不是 words.begin() + 1 因为 STL 使用 [begin, end].