是否可以从shared_ptr向量中删除元素?
Is it possible to remove elements from a vector of shared_ptr?
假设我有
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;
}
- 迭代时从向量和内存中删除对象
- 如何在向量中删除 std::function<void()>?
- 移除和删除与向量中的条件匹配的指针
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 如何在元组初始化向量中删除样板?
- 如何删除包含其他向量的向量?
- 从列表向量中删除无法按预期工作
- 从数组中删除非唯一值、保持顺序和不使用向量的最佳方法?
- 如何删除除 ArduinoSTL 的向量函数之外的所有函数
- 如何在具有特定条件的向量中删除所有元组?
- 删除指向抽象类的指针向量
- 从自定义数据类型向量中删除重复元素
- 防止我的向量在调用它的函数结束时被删除
- 从ints向量删除int的问题
- 使用find_if从向量删除所有偶数数字
- 崩溃,如果使用向量删除了一个嵌入式容器的destructor中删除char*成员
- 从向量删除重复元素
- 基元向量删除引发异常
- 如何使用指向每个元素的指针向量删除qcheckbox数组?