我尝试用邻接表表示法在C++中做图的实现。 我对C++中的“向量”并不陌生。 我得到了这个错误
class Vertex
{
private:
public:
int node_id;
string value;
vector< std::pair<Vertex* , int> > adj_list;
Vertex(int node_id, string value = "" )
{
this->node_id=node_id;
this->value=value;
}
};
接下来,我在下节课中做一个“顶点”的向量。 对于每个循环,都会发生此错误。 IntelliSense:“for each”语句不能对类型为“std::Vector
class graph
{
private:
public:
std::vector< Vertex* > vertex_list;
void add_node(int node_id, string value)
{
for each (auto var in vertex_list) //error here in the initialization of for each
{
if (var->node_id==node_id)
{
throw runtime_error("This node id exist already! put another id");
}
}
vertex_list.push_back(new Vertex(node_id, value));
}
}
我遵循了一个教程。 教程中的家伙有相同的代码,但没有出错。 我不确定如何解决这个情报问题。
您可以使用如下ranged-for循环(未经测试的代码)
for (auto& var : vertex_list) {
if (var->node_id==node_id) {
throw runtime_error("This node id exist already! put another id");
}
}