简体   繁体   English

c ++如何从函数中的向量中删除对象

[英]c++ how to remove objects from a vector in a function

I would like to remove objects from a vector. 我想从矢量中删除对象。 The vector is passed by reference, and I'm lost. 矢量通过引用传递,我迷路了。 I have tried brickList.erase(it) and also the combination erase remove, but I can't figure out the correct expression. 我已经尝试过brickList.erase(它)以及组合擦除删除,但我无法弄清楚正确的表达式。

Would someone be so kind to help me on the commented line? 有人会这么善意帮我评论一下吗? In the general case with let's say 100 bricks with random positions. 在一般情况下,让我们说100块砖随机位置。

#include "stdafx.h"

#include <iostream>
#include <vector>

class Brick {
private:
    int position;
public:
    Brick(int);
    int getPosition();
};
Brick::Brick(int p) : position(p) {};
int Brick::getPosition() { return position; }

void removeBricks(int a, int b, std::vector<Brick> &brickList) {
    std::vector<Brick>::iterator it;
    for (it = brickList.begin(); it != brickList.end(); ++it) {
        int currentPos = (*it).getPosition();
        if (currentPos >= a && currentPos <= b) {
            // here I need help to delete objects
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<Brick> myBricks;
    for (int i = 0; i<10; ++i) {
        Brick tmp_brick(i);
        myBricks.push_back(tmp_brick);
    }
    // delete all bricks with position between 3 and 5
    removeBricks(3, 5, myBricks);
    system("pause");
    return 0;
}

From your code 从你的代码

// delete all bricks with position between 3 and 5
//removeBricks(3, 5, myBricks);  // <-- replace this
myBricks.erase(myBricks.begin()+3,myBricks.begin()+5); // <--  with this

using the range based erase function. 使用基于范围的erase功能。 You don't need your removeBricks function at all really. 你根本不需要你的removeBricks函数。

If you have to use removeBricks() function you could do it like this using the same function prototype. 如果你必须使用removeBricks()函数,你可以使用相同的函数原型这样做。

void removeBricks(int a, int b, std::vector<Brick> &brickList) {
    // check a >= 0, a < b and b doesn't exceed length of brickList
    brickList.erase(brickList.begin()+a,brickList.begin()+b);
}

EDIT: seems that your notion of position does not refer to the array position of your items but the value of the member function of Brick . 编辑:似乎您的位置概念不是指您的项目的数组位置,而是Brick的成员函数的值。 This should work for you. 这应该适合你。

void removeBricks(int a, int b, std::vector<Brick> &brickList) 
{
    std::vector<Brick>::iterator it;
    for (it = brickList.begin(); it != brickList.end(); /* DONT increment here*/) {
        int currentPos = (*it).getPosition();
        if (currentPos >= a && currentPos <= b) {
            // store the return value from erase!!
            it = erase(it);
        }
        else
        {
            // increment here rather than in the for loop incrementer
            it++;
        }
    }
}

You have to store the return value from erase because the iterator gets invalidated. 您必须存储erase的返回值,因为迭代器无效。 The above should work for you although vector is not the best choice of container as erasing items can be slow due to the restructuring of the underlying array. 虽然vector不是容器的最佳选择,但上面应该对你有用,因为由于底层数组的重组,擦除项可能很慢。

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

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