c++ - 在图中使用迭代器时出错

标签 c++ graph stl

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 ;
        }
    }
}

我在 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|

,请帮帮我。

最佳答案

声明中不能有两个变量名称或类型。你的编译器希望你只说 std::list<Edge> iterator; .但是,您可能是这个意思,因为 iteratortypedef类(class)内std::list<Edge> :

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

但是请注意,您可以使用 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);
}

关于c++ - 在图中使用迭代器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444177/

相关文章:

c++ - 构造函数的参数命名

java - 为 Java 应用程序创建图形

c++ - 为什么 C++ 标准库没有具有连续内存的双端队列版本?

language-agnostic - 将图形数据表示为键值对象

c++ - 未排序 vector 上的 STL set_union 和 set_intersection

STL - 在 Objective-C++ 中存储 Obj-C 对象时,STL 容器是否支持 ARC?

java - 使用C++将类文件转换为jar文件

关于整数文字的 C++ Primer 段落,需要有人澄清一些要点

c++ - 使用 iPhone ObjectiveC Apps 中的 C/C++ 静态库

c++ - 如何清除C++代码中的SIGABRT错误