这个特定的载体是如何工作的

How is this specific vector working?

本文关键字:何工作 工作      更新时间:2023-10-16

您好,我得到了这段工作正常的代码,但我无法理解其中的某些部分,如果有人有空闲时间,我真的可以使用一些帮助。我不明白为什么矢量上有指针 *hightway,为什么矢量中有一颗星?我发现的载体都没有使用星星。即使是最轻微的帮助也是受欢迎的,提前感谢。

int main() {

    int menuSelection,entryPoint,exitPoint,vehicleType;
    string plate,entryTime,exitTime;
    Vehicle * v = NULL;
    vector<Vehicle *> * highway;
    highway = new vector<Vehicle*>;


    do {
        mainMenu:
        cout << "nntKENTPIKO MENOYn";
        cout << "t[1] Eisodos oximatosn";
        cout << "t[2] Eksodos oximatosn";
        cout << "t[0] Eksodos apo to programmann";
        cout << "tEpilogi: ";
        cin >> menuSelection;
        switch(menuSelection) {
        case 0:
            cout << "nnEuxaristoume pou xrisimopoihsate to programma mas!";
            return 0;
        case 1:
            cout << "nntEISODOS OXIMATOS";
            do {
                cout << "ntSe poio simeio briskeste ; (1 ews 3) t";
                cin >> entryPoint;
            } while(entryPoint > 3 || entryPoint == 0);
            cout << "ntArithmos kukloforias: ";
            cin >> plate;
            cout << "ntTupos oximatost";
            cout << "nt1: Dikiklat2:Autokinitat3:Fortigan";
            do {
                cout << "tEpilogi: ";
                cin >> vehicleType;
            }while (vehicleType > 3 || vehicleType == 0);
            cout << endl;
            if (vehicleType==1){
                v = new Motorcycle();
            }
            else if (vehicleType==2){
                v = new Car();
            }
            else if (vehicleType==3){
                v = new Truck();
            }
            v->setPlate(plate);
            v->setEntryPoint(entryPoint);
            v->setEntryTime(getTime());
            highway->push_back(v);
            cout << "nntTo autokinito mpike stis " << v->getEntryTime() << " apo to simeio " << v->getEntryPoint();
            cout << "nntParakalo paralabate to eisitirio";
            v = 0;
            goto mainMenu;
        case 2:
            cout << "nntEKSODOS OXIMATOS";
            cout << "ntArithmos kukloforias: ";
            cin >> plate;
            for(vector<Vehicle*>::iterator it = highway->begin(); it != highway->end(); it++){
                if (plate.compare((*it)->getPlate()) == 0){
                    do {
                        cout << "ntSe poio shmeio briskeste; (" << (*it)->getEntryPoint()+1 <<" ews 4) t";
                        cin >> exitPoint;
                    }while(exitPoint > 4 || exitPoint <= (*it)->getEntryPoint());
                    (*it)->setExitPoint(exitPoint);
                    (*it)->setExitTime(getTime());

                    cout << "tArithmos kukloforias: " << (*it)->getPlate() << endl << endl;
                    cout << "tSimeio eisodou: " << (*it)->getEntryPoint() << endl;
                    cout << "tOra eisodou : " << (*it)->getEntryTime() << endl;
                    cout << "tSimeio eksodou : " << (*it)->getExitPoint() << endl;
                    cout << "tOra eksodou  : " << (*it)->getExitTime() << endl << endl;
                    cout << "tSinoliki apostasi: " << (*it)->totalDistance() << "km"<< endl;
                    double km;
                    km=(*it)->totalFee()/(*it)->totalDistance();
                    cout << "ntkostos ana klm: " << km;
                    cout << "nntSINOLIKO POSO: " << (*it)->totalFee();
                    cout << "nntH apodeiksi einai ston ektupoti";
                }
                else {
                    cout << "nnTO OXIMA DEN YPARXEI!" << endl;
                    break;
                }
            }
            goto mainMenu;
        default:
            cout << "nntLATHOS EPILOGI! Dokimaste ksana.nn";
            goto mainMenu;
        }
    } while (menuSelection != 0);
    return 0;
}

我不明白为什么矢量上有指针*高路

因为它是指向vector<Vehicle *>的指针,所以在第highway = new vector<Vehicle*>;行中,您会看到它是使用分配给new的内存的指针初始化的。

为什么矢量中有一颗星星?

你的意思是Vehicle* ? 这是因为这个向量包含指向车辆对象的指针。每个元素都是指向某个动态分配的 Vehicle 对象的指针。

你真的不需要高速公路作为指针,你可以使用:

vector<Vehicle *>  highway;

后来使用highway.push_back而不是highway->push_back.