我希望有人可以帮助我。

我已经尝试修复此程序,但是我不知道该怎么办。我不断收到有关向量下标超出范围的错误。但是,我已经使用push_back向“ main”中创建的每个变量添加了一个对象,所以我不知道问题出在哪里。

问题是一种由9个城市组成的图表。每个城市都直接连接到一个城市。使用函数“ shortpath”,我将每个城市的父级添加到成员previous1。

但是,当我运行该程序时,我一直告诉我,即使在主目录中push_backed,城市的变量“ neighbor1”的大小也为0。

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;

class City
{
public:

City();
City( string city );
string cityName;
int cityDistance;
vector<City> neighbor1;
bool visited1;
vector<City> previous1;
void addNeighbor( City c );
void shortPath( vector<City> cities );
void printPath( City final );

};


这是头文件的实现:

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;
#include"City.h"



City::City()
{
cityDistance = -1;
cityName = " ";
visited1 = false;

}
City::City( string city )
{
cityDistance = -1;
cityName = city;
visited1 = false;


}

void City::addNeighbor( City c )
{
neighbor1.push_back( c );
}

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();

}




}





void City::printPath( City final )
{
if( final.previous1.size() != 0 )
{
    cout << final.previous1[0].cityName << endl;
}
else
{
    cout << "No previous for final" << endl;
}

}


这是主要的:

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;
#include"City.h"

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

HongKong.printPath( NewYork );







system("PAUSE");
return 0;
 }

最佳答案

您正在复制城市。

vector1.push_back(HongKong);中,不是将原始城市添加到vector1中,而是一个副本。

之后,您执行HongKong.neighbor1.push_back( NewDelhi);,该操作仅适用于原始文件,不适用于vector中的副本。

因此,当您在NewYork.shortPath( vector1 );中使用vector1时,它不包含邻居。

10-08 04:15