c++定义了一个数组并初始化为0之后,我的其他变量也变成0了

如题,定义check A、B数组之前G中有我读入的数据,cout正常;定义check数组并置0之后,G中的数据也全部变成0了。但是不给数组初始化的时候又是正常的。我又试着先不初始化之后写for循环给数组赋值,但是也不行,G中数据还是全部变成0
   cout<<G.airports[A-1].firstFlight->info.ID<<endl;
   int checkedA[100]={0},checkedB[100]={0};
   cout<<G.airports[A-1].firstFlight->info.ID<<endl;

2346
0

你把你具体的代码发出来。发这一小部分看不出来有什么问题。

img


debug的时候可以看到原本好好的东西全部变成0了

img

完整代码

main


#include <iostream>
#include "graph.hpp"

using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    Graph G;
    CreateGraph(G);
    ABconnected(G);
    return 0;
}

graph.hpp

#ifndef graph_hpp
#define graph_hpp

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

typedef struct flightInfo{
    int ID;
    string depDate;
    string IorD;
    int NO;
    int depAirport;
    int arrAirport;
    string depTime;
    string arrTime;
    int airplaneID;
    int airplaneMod;
    int fare;
};//存储航线信息

typedef struct flightNode{
    int arrAirport;                     //该弧所指向的机场
    struct flightNode *nextFlight;     //指向下一条弧的指针
    flightInfo info;                    //该弧相关信息
}flightNode;

typedef struct airportNode{
    int airport;                //该顶点对应机场
    flightNode *firstFlight;    //指向第一条从该机场出发的航线
}airportNode,AdjList[100];

typedef struct{
    AdjList airports;           //所有机场
    int airportN,flightN;       //当前机场数和航线数
} Graph;

//typedef struct Mflight{
//    int time;
//    int fare;
//    flightInfo info;
//}Mflight,mflightMatrix[100][100];
//
//typedef struct{
//    int Mairports[100];
//    mflightMatrix flights;
//    int airportN,flightN;
//} MGraph;


void CreateGraph(Graph &G);
void CreateFlightNode(string orgline,flightNode &fnode);

//void CreateMGraph(MGraph &G);
//void CreateMFlight(Mflight &mf);

int whetherAtoB(int A,int B,Graph G,int checked[100],int &count);
void ABconnected(Graph G);

#endif /* graph_hpp */


graph.cpp

#include "graph.hpp"

void CreateGraph(Graph &G){
    //采用邻接表表示法,构造有向网G
    G.airportN=79;G.flightN=2346;   //共有 2346 条航线,涉及 79 个机场
    for (int i=0;i<79;i++){
        G.airports[i].airport=i+1;  //输入机场编号
        G.airports[i].firstFlight=NULL; //第一条航线暂时置空
    }//for 初始化airports
    
    //构造邻接表
    ifstream infoF("/Users/fumeiziyi/Downloads/数据结构与算法/实验六/实验6-航空数据.csv");
    string wuyong;
    getline(infoF,wuyong);
    string line;
    while (getline(infoF,line)) {
        flightNode temp;
        CreateFlightNode(line, temp);           //创建一条新的弧
        int curAirport=temp.info.depAirport-1;  //当前机场在airports中的位置
        //头插法
        temp.nextFlight=G.airports[curAirport].firstFlight;     //temp的下一条航线为原本该机场的第一条航线
        flightNode *p;
        p=&temp;
        G.airports[curAirport].firstFlight=p;               //该机场的第一条航线变为该航线
    }
}

void CreateFlightNode(string orgline,flightNode &fnode){
    //创建航线弧
    istringstream sin(orgline);
    vector<string> fields;
    string field;
    while (getline(sin, field,',')) {
        fields.push_back(field);        //现在整行的信息分别存储在fields中
    }
    
//    int ID
    fnode.info.ID=atoi(fields[0].c_str());
//    string depDate;
    fnode.info.depDate=fields[1];
//    string IorD;
    fnode.info.IorD=fields[2];
//    int NO;
    fnode.info.NO=atoi(fields[3].c_str());
//    int depAirport;
    fnode.info.depAirport=atoi(fields[4].c_str());
//    int arrAirport;
    fnode.info.arrAirport=atoi(fields[5].c_str());
//    string depTime;
    fnode.info.depTime=fields[6];
//    string arrTime;
    fnode.info.arrTime=fields[7];
//    int airplaneID;
    fnode.info.airplaneID=atoi(fields[8].c_str());
//    int airplaneMod;
    fnode.info.airplaneMod=atoi(fields[9].c_str());
//    int fare;
    fnode.info.fare=atoi(fields[10].c_str());
    
    fnode.arrAirport=fnode.info.arrAirport;
    fnode.nextFlight=NULL;
}

//void CreateMGraph(MGraph &G){
//
//}

int whetherAtoB(int A,int B,Graph G,int checked[100],int &count){
    //从A机场是否能到达B机场
    count++;        //深度加1
    if (A==B) {
        return 1;
    }//已经到达
    if (checked[A-1]==1) {
        return 0;
    }//该机场已经访问过(形成回路)
    checked[A-1]=1;
    flightNode *p=G.airports[A-1].firstFlight;    //从该机场的第一条航线开始
    while (p!=NULL) {
        int next=p->arrAirport;                 //该航线的到达机场
        if(whetherAtoB(next, B, G, checked,count)||checked[A-1]==0){
            return 1;
        }       //下一机场是否能到达
        p=p->nextFlight;                        //下一条航线
    }
    return 0;
}

void ABconnected(Graph G){
    //判断AB是否相互连通
    int A;int B;
    cout<<"请输入A机场号:";
    cin>>A;
    cout<<"请输入B机场号:";
    cin>>B;
    if (A==B) {
        cout<<"两次输入为同一机场!"<<endl;
        return;
    }
    int countA=-1,countB=-1;
    cout<<G.airports[A-1].firstFlight->info.ID<<endl;
    int checkedA[100]={0},checkedB[100]={0};
    cout<<G.airports[A-1].firstFlight->info.ID<<endl;
    int w1=whetherAtoB(A, B, G, checkedA, countA);
    int w2=whetherAtoB(B, A, G, checkedB, countB);
    if (w1==1&&w2==1) {
        cout<<"A机场与B机场可连通!"<<endl;
        if(countA==1){
            cout<<"从A机场可直飞到达B机场。"<<endl;
        }else{
            cout<<"从A机场经过"<<countA-1<<"次中转到达B机场。"<<endl;
        }
        if(countB==1){
            cout<<"从B机场可直飞到达A机场。"<<endl;
        }else{
            cout<<"从B机场经过"<<countB-1<<"次中转到达A机场。"<<endl;
        }
    }else{
        cout<<"A机场与B机场不可连通!"<<endl;
        if(w1==1){
            if(countA==1){
                cout<<"从A机场可直飞到达B机场。"<<endl;
            }else{
                cout<<"从A机场经过"<<countA-1<<"次中转到达B机场。"<<endl;
            }
        }else{
            cout<<"从A机场不可到达B机场。"<<endl;
        }
        if(w2==1){
            if(countB==1){
                cout<<"从B机场可直飞到达A机场。"<<endl;
            }else{
                cout<<"从B机场经过"<<countB-1<<"次中转到达A机场。"<<endl;
            }
        }else{
            cout<<"从B机场不可到达A机场。"<<endl;
        }
    }
    
    return;
}

void shortestTime(Graph G){
    
}


肯定是越界了