矢量擦除范围之外的迭代器

时间:2016-03-17 16:55:06

标签: c++ opengl vector glut

link到项目 我正在创建一个小型过剩应用程序,它将绘制电路组件。 我有一个名为“Component”的类看起来像这样:

// nextIndex is the index of the next component to be created
int Component::nextIndex = 0;
Component::Component( void ){
setType( RESISTOR );
setValue( 0.0 );
setX( 0.0 );
setY( 0.0 );
setSize( 2.0 );
setIndex( nextIndex );
}

eType Component::getType( void ){return cType}
double Component::getValue( void ){return cValue;}
double Component::getX( void ){return cX;}
double Component::getY( void ){return cY;}
double Component::getSize( void ){return cSize;}
void Component::setType( eType type ){cType = type;}
void Component::setValue( double value ){cValue = value;}
void Component::setX( double x){cX = x;}
void Component::setY( double y ){cY = y;}
void Component::setSize( double size ){cSize = size;}
int Component::getIndex( void ){return index;}
void Component::setIndex( int n ){index = n;}

注意:每次创建类的新实例时,我都会递增nextIndex。 这是我的另一个类,它有一个组件向量,并处理组件的添加和删除:

class OGWindow{
private:

    bool LINK, DELETE;
    int linkNo;
    ...
    Component currentSquare;
    Component *currentSquarePtr, *linkSquPtr1, *linkSquPtr2;
    double squareXStart, squareYStart, squareXEnd, squareYEnd;
    std::vector< Component > compVector;

public:
    ...
    void myMouseClick(int button, int state, int x, int y);
    ...
    void deleteSquare( Component *squPtr );
};

我正在为窗口创建组件:     int NEXT_INT = 0;

if (x >= createButtonX && x <= createButtonX + createButtonWidth) {
    cout << "Clicked Create button" << endl;
    Component *compPtr = new Component;
    compPtr->setX(0.0);
    compPtr->setY(0.0);
    compPtr->setIndex(NEXT_INT);
    NEXT_INT++;

    compVector.push_back(*compPtr);
    //------------SET THE CURRENT SQUARE TO THE NEWLY CREATED COMPONENT-----
    vector<Component>::iterator constIterator;
    for (constIterator = compVector.begin(); constIterator != compVector.end(); ++constIterator){
        currentSquarePtr = &(*constIterator);
    }
    cout << "Vector size:  " << compVector.size() << endl;
    glutPostRedisplay();
}

但是当我删除时,它删除了一些元素,在最后一个元素中我得到了“向量超出范围”错误:

void OGWindow::deleteSquare( Component *squPtr ) {
    compVector.erase(compVector.begin() + (squPtr->getIndex()));
cout << "Vector size:  " << compVector.size() << endl;
glutPostRedisplay();
}

请分享为何发生这种情况的想法。

2 个答案:

答案 0 :(得分:1)

您对Component对象的索引存在一些问题。

首先,向量中的索引就像数组,即基于零。因此,如果你有一个只有一个元素的向量,那么索引(和begin()迭代器的偏移量)是。其他任何事情都是出界的。

您似乎有一个static成员变量用于下一个索引,并且您将该变量初始化为1,这将始终是向量中的第一个索引。然后在Component构造函数中增加此静态nextIndex变量,使您创建的第二个对象具有索引2,但在向量中它将具有索引1(如果您有向量中的两个对象)。

从向量中移除对象时会出现另一个问题。除了使用错误索引的(现在显而易见的)问题之外,您不需要对向量中的索引重新编号。因此,如果向量中有两个元素(错误)索引12,并删除第一个元素,那么新的第一个元素将具有偶数wronger索引2

我解决这个问题的建议是根本不使用索引,而是实现比较运算符的相等性。然后你可以使用例如std::find在向量中找到对象,然后使用erase返回的正确迭代器调用std::find

然后deleteSquare看起来像

void OGWindow::deleteSquare( Component const &squRef ) {
    auto iterator = std::find( std::begin(compVector), std::end(compVector), squRef);

    if (iterator != std::end(compVector))
        compVector.erase(iterator);

    cout << "Vector size:  " << compVector.size() << endl;
    glutPostRedisplay();
}

答案 1 :(得分:1)

创建组件时会发生大量内存泄漏。您在堆上分配一个新组件,然后将其副本放入std::vector。在作用域退出后,指向堆上新组件的指针将永远丢失。

只需在堆栈上创建组件,将它们添加到您的矢量中即可。

此外,指数从0开始,而不是从1开始:

int Component::nextIndex = 1;

应该是

int Component::nextIndex = 0;

但无论如何这是一个设计错误;从std::vector中删除组件时,您必须更新所有组件的索引,这些组件位于已删除组件之后(您需要将其值减1)。 / p>

如果你开始考虑迭代器而不是指针,那会更好。我猜你想要以下内容:只是一个巨大的全球数据结构,其中包含所有组件。这可能是std::list<Component> compList。然后忘记管理索引和指针,并在任何地方使用std::list<Component>::iterator而不是Component*。只需执行

,即可安全地删除组件
void OGWindow::deleteSquare( std::list<Component>::iterator squPtr ) {
    compList.erase(squPtr);
    cout << "List size:  " << compList.size() << endl;
    glutPostRedisplay();
}

并添加组件

if (x >= createButtonX && x <= createButtonX + createButtonWidth) {
    cout << "Clicked Create button" << endl;
    Component comp;
    comp.setX(0.0);
    comp.setY(0.0);

    // Notice how we put the new component at the front
    // instead of the back.
    compList.push_front(comp);

    //------------SET THE CURRENT SQUARE TO THE NEWLY CREATED COMPONENT-----
    // easy now!
    currentSquarePtr = compList.begin();

    cout << "List size:  " << compList.size() << endl;
    glutPostRedisplay();
}

请记住,更改这些

Component *currentSquarePtr;
Component *linkSquPtr1;
Component *linkSquPtr2;

到这些

std::list<Component>::iterator currentSquarePtr;
std::list<Component>::iterator linkSquPtr1;
std::list<Component>::iterator linkSquPtr2;