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;
.但是,您可能是这个意思,因为 iterator
是 typedef
类(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/