是否可以从shared_ptr向量中删除元素?

Is it possible to remove elements from a vector of shared_ptr?

本文关键字:向量 删除 元素 ptr shared 是否      更新时间:2023-10-16

假设我有

vector<shared_ptr<string>> enemy;

如何从敌人矢量中删除元素?

提前感谢您的帮助

**编辑(上下文中的代码)

void RemoveEnemy( vector<shared_ptr<Enemy>> & chart, string id )
{
int i = 0;
bool found = FALSE;
for(auto it = chart.begin(); it != chart.end(); i++)
{
if(id == chart[i]->GetEnemyID() )
{
found = TRUE;
chart.erase(it);
}
}

上面的代码让我出错

删除元素的方式与从任何std::vector中删除任何元素的方式相同 - 例如,通过std::vector::erase()方法。 您所需要的只是对要删除的所需元素的iterator

在您的情况下,由于您存储的是std::shared_ptr<std::string>对象而不是存储实际的std::string对象,因此您可能需要使用std::find_if()之类的东西来查找包含所需字符串值的向量元素,例如:

void removeEnemy(string name)
{
auto iter = std::find_if(enemy.begin(), enemy.end(),
[&](auto &s){ return (*s == name); }
);
if (iter != enemy.end())
enemy.erase(iter);
}

更新:在您添加的新代码中,您错误地将索引和迭代器混合在一起。如果vector不为空,则正在创建一个无限循环,因为您从不递增控制循环的it迭代器,而是递增索引i变量(看看当你不给你的变量唯一和有意义的名称时会发生什么? 所以你最终会超越vector进入周围的记忆。 这就是您出现段错误的原因。

即使您(尝试)使用迭代器遍历vector,您正在使用索引来访问元素,而不是取消引用迭代器来访问元素。 在这种情况下,您根本不需要使用索引,仅迭代器就足够了。

试试这个:

void RemoveEnemy( vector<shared_ptr<Enemy>> & chart, string id )
{
for(auto it = chart.begin(); it != chart.end(); ++it)
{
if (id == it->GetEnemyID() )
{
chart.erase(it);
return;
}
}

或者,使用我之前建议的代码类型:

void RemoveEnemy( vector<shared_ptr<Enemy>> & chart, string id )
{
auto iter = std::find_if(chart.begin(), chart.end(),
[&](auto &enemy){ return (enemy->GetEnemyID() == id); }
);
if (iter != chart.end())
chart.erase(iter);
}

代码的问题在于erase()使迭代器无效。您必须使用it = chart.erase(it).

我喜欢我的,它会高速移除外星人,而不关心其他物品的订购。带着偏见移除!

注意:remove_if最常与erase一起使用,它将保留其余元素的顺序。但是,partition不关心元素的顺序,而且速度要快得多。

分区测试.cpp:
make partition-test && echo 1 alien 9 alien 2 8 alien 4 7 alien 5 3 | ./partition-test

#include <algorithm>
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &container) {
bool comma = false;
for (const auto &x : container) {
if (comma)
os << ", ";
os << *x;
comma = true;
}
return os;
}
int main() {
vector<shared_ptr<string>> iv;
auto x = make_shared<string>();
while (cin >> *x) {
iv.push_back(x);
x = make_shared<string>();
}
cout << iv << 'n';
iv.erase(partition(begin(iv), end(iv),
[](const auto &x) { return *x != "alien"s; }),
end(iv));
cout << iv << 'n';
return 0;
}