Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向量迭代器不兼容 我目前正在为C++图形库工作,现在在运行时在调试模式中得到断言错误。我也看了一些其他的问题,但没有一个问题和答案能让我找到答案。在一些论坛中阅读后,我有这样的印象,即发生此错误是因为一旦向量内容发生更改,迭代器就会失效。(例如,当使用erase())但正如您在我的代码中看到的,我并没有修改向量,只是迭代_C++_Stl_Vector_Iterator_Stdvector - Fatal编程技术网

向量迭代器不兼容 我目前正在为C++图形库工作,现在在运行时在调试模式中得到断言错误。我也看了一些其他的问题,但没有一个问题和答案能让我找到答案。在一些论坛中阅读后,我有这样的印象,即发生此错误是因为一旦向量内容发生更改,迭代器就会失效。(例如,当使用erase())但正如您在我的代码中看到的,我并没有修改向量,只是迭代

向量迭代器不兼容 我目前正在为C++图形库工作,现在在运行时在调试模式中得到断言错误。我也看了一些其他的问题,但没有一个问题和答案能让我找到答案。在一些论坛中阅读后,我有这样的印象,即发生此错误是因为一旦向量内容发生更改,迭代器就会失效。(例如,当使用erase())但正如您在我的代码中看到的,我并没有修改向量,只是迭代,c++,stl,vector,iterator,stdvector,C++,Stl,Vector,Iterator,Stdvector,错误在我用//ASSERTION标记的行中。奇怪的是,neighbor\u it并没有指向(*vertex\u it)->neighbor()中的第一个对象,而是指向0xfeeefeee。在调试代码时,我可以清楚地看到邻居向量至少包含一项。它不应该指向这个向量中的第一个对象吗 有关详细信息:m_顶点是图中所有顶点的向量,vertex::neighbories()返回边向量(其指针指向邻居/目标顶点)。在这种方法中,我希望删除指向某个顶点的所有边。如果找到并删除了相应的边,则返回true;如果没有

错误在我用
//ASSERTION
标记的行中。奇怪的是,
neighbor\u it
并没有指向
(*vertex\u it)->neighbor(
)中的第一个对象,而是指向
0xfeeefeee
。在调试代码时,我可以清楚地看到邻居向量至少包含一项。它不应该指向这个向量中的第一个对象吗

有关详细信息:
m_顶点
是图中所有顶点的向量,
vertex::neighbories()
返回边向量(其指针指向邻居/目标顶点)。在这种方法中,我希望删除指向某个顶点的所有边。如果找到并删除了相应的边,则返回true;如果没有指向p_顶点的边,则返回false

bool graph::remove_edges_pointing_to( vertex* p_vertex )
{
    bool res = false;

    std::vector<vertex*>::iterator vertex_it = m_vertices.begin();

    // iterate through all vertices
    while( vertex_it != m_vertices.end() )
    {
        // iterator on first element of neighbors of vertex
        std::vector<edge*>::iterator neighbor_it = (*vertex_it)->neighbors().begin();

        // iterate through all successors of each vertex
        while( neighbor_it != (*vertex_it)->neighbors().end() ) //ASSERTION
        {
            if( (*neighbor_it)->dest() == p_vertex )
            {
                if( (*vertex_it)->remove_edge( *neighbor_it ) )
                {
                    res = true;
                }
            }

            neighbor_it++;
        }

        vertex_it++;
    }

    return res;
}

再次感谢您的回答!:)

我猜
remove\u edge
会修改
neighbor\u it
的底层容器,从而使其无效,但是如果没有看到更多的代码,我无法确定


如果是这种情况,一个可能的解决方案是将迭代器返回到已删除元素之后的下一个元素,例如通过
std::vector::erase
我猜,鉴于您提供的有限上下文,
neights()
返回
std::vector
的副本,而不是引用,即
std::vector&
。所以在
begin()
调用临时对象之后,迭代器指向垃圾。

remove\u edge()做什么?它是否接触循环中使用的任何数据?如何定义
邻居()
?删除边将从它指向的顶点删除边(邻居关系)。但是当启动断言时,我甚至没有到达调用remove_edge的那一行。这是一个std::vector,包含一个顶点的邻域关系。@Exa:那么这个问题可能是在调用整个函数之前就出现的,因为我在这里没有看到任何会导致它的东西。可能是初学者的错误,但是的,就是这样。我将研究参考,以避免这些错误在未来,非常感谢!是的,这是另一个问题。删除后,迭代器将无效。我必须弄清楚如何在删除一条边后继续迭代。非常感谢。
bool graph::remove_edges_pointing_to( vertex* p_vertex )
{
    bool res = false;

    std::vector<vertex*>::iterator vertex_it = m_vertices.begin();

    // iterate through all vertices
    while( vertex_it != m_vertices.end() )
    {
        // iterator on first element of neighbors of vertex
        std::vector<edge*>::iterator neighbor_it = (*vertex_it)->neighbors().begin();

        // iterate through all successors of each vertex
        while( neighbor_it != (*vertex_it)->neighbors().end() )
        {
            if( (*neighbor_it)->dest() == p_vertex )
            {
                neighbor_it = (*vertex_it)->remove_edge( *neighbor_it );
                res = true;
            }
            else
            {
                neighbor_it++;
            }
        }

        vertex_it++;
    }

    return res;
}