简体   繁体   English

在图形中使用迭代器时出错

[英]error using iterators in graph

void Graph::removeEdge( int u , Edge e )
{
    for( std::list<Edge> iterator i = AdjList[u].begin() ;
                             i != AdjList[u].end() ;
                             ++i )
    {
        if( i->vertex() == e.vertex() )
        {
            AdjList[u].erase(i) ;
            break ;
        }
    }
}

I have used this function in a graph class and during compiling I am getting the following errors 我在graph类中使用了此函数,并且在编译过程中出现以下错误

|In member function ‘void Graph::removeEdge(int, Edge)’:|
|47|error: expected ‘;’ before ‘i’|
|47|error: ‘i’ was not declared in this scope|
|48|error: expected ‘)’ before ‘;’ token|
|49|error: ‘i’ was not declared in this scope|
|49|error: expected ‘;’ before ‘)’ token|
|59|error: expected ‘}’ at end of input|

, please help me out . ,请帮帮我。

You can't have two variable names or types in a declaration. 声明中不能有两个变量名或类型。 Your compiler expects you just to say std::list<Edge> iterator; 您的编译器希望您只说std::list<Edge> iterator; . However, you probably meant this, since iterator is a typedef within the class std::list<Edge> : 但是,由于iterator是类std::list<Edge>typedef ,您可能会这样说:

std::list<Edge>::iterator i = AdjList[u].begin();

However, note that you can use std::find_if to locate the element: 但是,请注意,您可以使用std::find_if查找元素:

std::list<Edge>::iterator it = std::find_if(AdjList[u].begin(), AdjList[u].end(), 
    [&e](const Edge &edge) {return edge.vertex() == e.vertex();}
);

if (it != AdjList[u].end()) {
    AdjList[u].erase(it);
}

问题是您在这里缺少::

for( std::list<Edge>::iterator i = AdjList[u].begin() ;

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

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