通过索引从向量中删除对象

时间:2016-11-26 15:54:26

标签: c++ vector

我有一个看起来像这样的for循环:

for (int i = Particles.size() - 1; i >= 0; i--) {
    if (Particles[i].Dead) {
        Particles.erase(Particles.begin() + i);
    }
}

编译时,我收到此错误:

  

错误C2280'粒子&粒子::运算符=(const粒子&)':尝试引用已删除的函数
  重力C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ include \ xutility 2518

任何人都知道我做错了什么?这是粒子代码。

标头文件

class Particle
{
public:
    const float G = 6.674 * pow(10, -11);
    float Mass, Radius;
    Vector2 Position, Velocity, Force;
    bool Dead = false;

    void Gravity(Particle Particle2);
    void Move();
    void Draw(SDL_Surface *Surface, Uint32 Color);

    Particle(float MassBounds[2], Vector2 PositionBounds[2], float Density);
    Particle::Particle(float Mass, Vector2 Position, float Density, bool NonRandom);
    Particle();
};

源文件

void Particle::Gravity(Particle Particle2)
{
    float GravityMagnitude = (Particle::G*Mass*Particle2.Mass) / Vector2::DistanceSquared(Position, Particle2.Position);
    Force += (Particle2.Position - Position).Normalised()*GravityMagnitude;
}

void Particle::Move()
{
    Velocity += Force/Mass;
    Position += Velocity;
}

void Particle::Draw(SDL_Surface *Surface, Uint32 Color)
{
    if (int(Radius) > 0) { SDLDrawFilledCircle(Surface, int(Position.x), int(Position.y), Radius<1?1:int(Radius), Color); }
}

Particle::Particle(float MassBounds[2], Vector2 PositionBounds[2], float Density)
{
    Mass = RandRange(MassBounds);
    Position = Vector2(RandRange(PositionBounds[0].x, PositionBounds[1].x), RandRange(PositionBounds[0].y, PositionBounds[1].y));
    Radius = pow((3.0 * Mass) / (4 * M_PI*Density), 1.0 / 3.0);
    Velocity = Vector2();
    Force = Vector2();
}
Particle::Particle(float Mass, Vector2 Position, float Density, bool NonRandom)
{
    this->Mass = Mass;
    this->Position = Position;
    Radius = pow((3.0 * Mass) / (4 * M_PI*Density), 1.0 / 3.0);
    Velocity = Vector2();
    Force = Vector2();
}
Particle::Particle()
{
    Mass = 1;
    Position = Vector2();
    Radius = 1;
    Velocity = Vector2();
    Force = Vector2();
}

inline bool operator==(const Particle& a, const Particle& b) { return a == b; }

2 个答案:

答案 0 :(得分:0)

使用remove_if和erase可能会有更好的运气:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Vector {
    int x;
    int y;
};

bool is_negative (Vector a) {
    return a.x < 0 && a.y < 0;
}

int main() {
    vector<Vector> vectors;
    vectors.push_back({-1, -2});
    vectors.push_back({-1, 2});
    vectors.push_back({1, 2});
    vectors.push_back({-1, -2});
    vectors.erase(
        std::remove_if(vectors.begin(), vectors.end(), is_negative),
        vectors.end());

    for (auto v : vectors) {
        cout << v.x << " " << v.y << endl;
    }
}

答案 1 :(得分:-1)

我在你的for循环中无法注意到这一点:

Particles.erase(Particles.begin() + i-1)

更重要的是

i - 1 

你从

开始
i = Particles.size() - 1 

并且直到

才停止
i < 0  

Particles.begin() 

给你列表的头部。
什么时候

i == 0  

Particles.begin() + (i - 1) 

将尝试获取无效节点 转换为

 Particles.begin() - 1  

我不知道你的代码何时中断,我将基于你的循环

以下是w / doc http://www.cplusplus.com/reference/vector/vector/erase/

的链接

希望这有帮助