C++ 在迭代c+期间从向量中删除元素+;

C++ 在迭代c+期间从向量中删除元素+;,c++,vector,iterator,C++,Vector,Iterator,我编写此方法是为了找到稀疏矩阵的次方: SpMatrixVec SparseMatrix::minor(SpMatrixVec matrix, int col) const{ SpMatrixVec::iterator it = matrix.begin(); int currRow = it->getRow(); int currCol = col; while(it != matrix.end()) { if(it->ge

我编写此方法是为了找到稀疏矩阵的次方:

SpMatrixVec SparseMatrix::minor(SpMatrixVec matrix, int col) const{

    SpMatrixVec::iterator it = matrix.begin();

    int currRow = it->getRow();

    int currCol = col;

    while(it != matrix.end()) {

        if(it->getRow() == currRow || it->getCol() == currCol){
            matrix.erase(it);
        }
        // if we have deleted an element in the array, it doesn't advance the
        // iterator and size() will be decreased by one.
        else{   
            it++;
        }

    }

    // now, we alter the cells of the minor matrix to be of proper coordinates.
    // this is necessary for sign computation (+/-) in the determinant recursive
    // formula of detHelper(), since the minor matrix non-zero elements are now
    // in different coordinates. The row is always decreased by one, since we
    // work witht he first line, and the col is decreased by one if the element
    // was located after 'col' (which this function receives as a parameter).

    //change the cells of the minor to their proper coordinates.
    for(it = matrix.begin(); it != matrix.end(); it++){

        it->setRow(it->getRow()-1);

        int newY;
        newY = (it->getCol() > col) ? it->getCol() + 1 : it->getCol();

        it->setCol(newY);
    }
    return matrix;

}
现在,我可能做错了什么,因为当到达
while
循环的第二次迭代时,程序崩溃了。 基本思想是检查向量,看它是否是相关坐标,如果是,则删除它。我只在没有删除的情况下增加迭代器(在这种情况下,向量应该更新迭代器以指向下一个元素..除非我把这些事情搞错了)

问题在哪里


非常感谢。

在元素之间迭代时,不能更改集合。 使用另一个临时集合存储部分结果

编辑:更好的是,使用函子删除元素:
remove\u if

编写条件。

在元素之间迭代时,不能更改集合。 使用另一个临时集合存储部分结果

编辑:更好的是,使用函子删除元素:
remove\u if

您编写条件。

擦除使迭代器无效。执行
it=matrix.erase(it)

erase
使迭代器无效。执行
it=matrix.erase(it)

erase()
使迭代器无效

必须使用
erase()
的返回值更新
it
,循环才能工作:

while(it != matrix.end()) {

    if(it->getRow() == currRow || it->getCol() == currCol){
        //matrix.erase(it);
        it = matrix.erase(it); // Here is the change
    }
    // if we have deleted an element in the array, it doesn't advance the
    // iterator and size() will be decreased by one.
    else{   
        //it++;
        ++it; // ++i is usually faster than i++. It's a good habit to use it.
    }

}
erase()
使迭代器无效

必须使用
erase()
的返回值更新
it
,循环才能工作:

while(it != matrix.end()) {

    if(it->getRow() == currRow || it->getCol() == currCol){
        //matrix.erase(it);
        it = matrix.erase(it); // Here is the change
    }
    // if we have deleted an element in the array, it doesn't advance the
    // iterator and size() will be decreased by one.
    else{   
        //it++;
        ++it; // ++i is usually faster than i++. It's a good habit to use it.
    }

}

SpMatrixVec和SparseMatrix是如何定义的?它++:(++it:)当然这不是问题,但问题已经得到了正确的解决。如果这是一个向量,您最好使用它,因为这样只需复制一次,然而,代码中的每一次擦除都必须复制以下所有元素。SpMatrixVec和SparseMatrix是如何定义的?它++:(++it:)当然这不是问题,但问题已经得到了正确的解决。如果这是一个向量,你最好使用它,因为这样只需复制一次,而代码中的每一次擦除都必须复制以下所有元素,这完全是错误的。你可以在迭代时修改它,你只需要确保迭代器在执行时不会失效。不,这是可能的。您只需存储
erase
的返回值即可获得新的有效迭代器。这完全是错误的。你可以在迭代时修改它,你只需要确保迭代器在执行时不会失效。不,这是可能的。您只需存储
erase
的返回值,即可获得新的有效迭代器。@Steve Townsend:当然,我没有检查代码中是否存在任何其他“错误”,但您确实完全正确。固定的。谢谢。这段代码有点危险,您必须检测迭代器是否指向向量中的最后一个元素,并进行专门处理。通过这个例子,我以艰苦的方式学会了这一点;-)@本杰:我不明白。到达最后一个元素时会有什么危险?如果您删除
,新的
成为
矩阵.end()
,则循环条件不满足,控制退出循环。@Steve Townsend:当然,我没有检查代码中的任何其他“错误”,但您确实完全正确。固定的。谢谢。这段代码有点危险,您必须检测迭代器是否指向向量中的最后一个元素,并进行专门处理。通过这个例子,我以艰苦的方式学会了这一点;-)@本杰:我不明白。到达最后一个元素时会有什么危险?如果删除
it
,新的
it
变为
matrix.end()
,则循环条件不满足,控制退出循环。