這個問題在這里已經有了答案: 從 std::vector 擦除范圍? (4 個回答) 11 小時前關閉。
我有一個充滿單詞的向量,我試圖在指定的開始和結束處擦除該向量的一大塊。例如:
#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 函式或快速單行函式?
uj5u.com熱心網友回復:
使用vector::erase:
words.erase(words.begin(), words.begin() 2);
注意:提供的第二個迭代器是words.begin() 2而不是words.begin() 1因為 STL 使用 [begin, end)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/382470.html
下一篇:工廠設計與繼承
