错误:矢量下标超出范围c ++

时间:2013-05-25 08:22:29

标签: c++ visual-c++ pointers vector

我希望有人能帮助我。

我试图修复这个程序,但我不知道还有什么。关于矢量的下标超出范围,我一直收到这个错误。但是,我已经使用push_back为在“main”中创建的每个变量添加了一个对象,所以我不知道问题是什么。

问题是一种由9个城市组成的图表。每个城市都与一个城市直接相连。使用“短路径”功能,我将每个城市的父级添加到成员previous1。

然而,当我运行该程序时,我一直告诉我,即使我在主要部分推回,城市变量“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;
 }

1 个答案:

答案 0 :(得分:1)

您正在复制城市。

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

之后,您执行的HongKong.neighbor1.push_back( NewDelhi);仅适用于原始版本,而不适用于vector中的副本。

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