我正在尝试制作一个直接连接 9 个城市的图表。我在使用“shortpath”功能时遇到了这个问题。我不断收到错误“向量迭代器不兼容”,我无法弄清楚。请提供任何帮助。谢谢
这是功能:
void City::shortPath( vector<City*> & cities )
{
queue<City*> q;
q.push(this);
for( unsigned int i = 0; i < cities.size(); i++ )
{
cities[i]->cityDistance = -1;
cities[i]->visited1 = false;
}
q.front()->visited1 = true;
q.front()->cityDistance = 0;
while( !q.empty() )
{
City * v = q.front();
if( v->neighbor1.size() != 0 )
{
for( unsigned int i = 0; i < v->neighbor1.size(); i++ )
{
City * z = v->neighbor1[i];
q.push( z );
if( z->visited1 == false )
{
v->neighbor1[i]->cityDistance = v->cityDistance + 1;
v->neighbor1[i]->previous1.push_back( *v );
v->neighbor1[i]->visited1 = true;
}
}
}
q.pop();
}
}
这是主要的:
int main()
{
City * NewDelhi = &City( "New Delhi" );
City * HongKong = &City("Hong Kong");
City * Washington = &City("Washington");
City * Dublin = &City("Dublin");
City * Lisbon = &City("Lisbon");
City * Vienna = &City("Vienna");
City * Santiago = &City("Santiago");
City * RioDeJaneiro = &City("RioDeJaneiro");
City * Berlin = &City( "Berlin" );
City * NewYork = &City( "NewYork" );
vector<City*> vector1;
vector1.push_back(HongKong);
vector1.push_back(NewDelhi);
vector1.push_back(Washington);
vector1.push_back(Dublin);
vector1.push_back(Lisbon);
vector1.push_back(Vienna);
vector1.push_back(Santiago);
vector1.push_back(RioDeJaneiro);
vector1.push_back(Berlin);
vector1.push_back(NewYork);
(*HongKong).neighbor1.push_back( NewDelhi );
(*NewDelhi).neighbor1.push_back( Washington);
(*Washington).neighbor1.push_back( Dublin );
(*Dublin).neighbor1.push_back( Lisbon );
(*Lisbon).neighbor1.push_back( Vienna );
(*Vienna).neighbor1.push_back( Santiago );
(*Santiago).neighbor1.push_back( RioDeJaneiro );
(*RioDeJaneiro).neighbor1.push_back( Berlin );
(*Berlin).neighbor1.push_back( NewYork );
(*NewYork).neighbor1.push_back( HongKong );
(*NewYork).shortPath( vector1 );