简体   繁体   English

如何从 Vector 中删除某个部分?

[英]How do I delete a certain section from a Vector?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector <string> items = {"Sword\n", "Shield\n", "Brass Helmet\n", "Iron Breastplate\n"};

void inventory(){

     for (int i = 0; i < items.size(); i++){
          cout<<items[i];
     }
}

int main(){
    inventory();
    return 0;
}

I'm familiar with the commands vector.pop_back() which deletes the last in the vector, and vector.clear() which deletes the entire vector.我熟悉删除向量中最后一个的命令 vector.pop_back() 和删除整个向量的 vector.clear() 命令。

How would I go about deleting items[0] or deleting the portion items[0] - items[3]?我将如何 go 关于删除项目 [0] 或删除部分项目 [0] - 项目 [3]?

As the guys told you, you can use the erase function.正如人们告诉你的那样,你可以使用擦除 function。 You can use it to erase a specific element from the vector or delete a portion of it.您可以使用它从向量中擦除特定元素或删除其中的一部分。

items.erase(items.begin());
items.erase(items.begin(), items.begin() + 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM