【问题标题】:How is this specific vector working?这个特定的载体是如何工作的?
【发布时间】:2016-01-19 22:40:22
【问题描述】:

您好,我得到了这个运行良好的代码,但我无法理解其中的某些部分,如果有人有空的话,我真的可以使用一些帮助。我不明白为什么向量上有一个指针*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 << "\n\n\tKENTPIKO MENOY\n";
        cout << "\t[1] Eisodos oximatos\n";
        cout << "\t[2] Eksodos oximatos\n";
        cout << "\t[0] Eksodos apo to programma\n\n";
        cout << "\tEpilogi: ";
        cin >> menuSelection;

        switch(menuSelection) {
        case 0:

            cout << "\n\nEuxaristoume pou xrisimopoihsate to programma mas!";
            return 0;
        case 1:

            cout << "\n\n\tEISODOS OXIMATOS";

            do {
                cout << "\n\tSe poio simeio briskeste ; (1 ews 3) \t";
                cin >> entryPoint;
            } while(entryPoint > 3 || entryPoint == 0);

            cout << "\n\tArithmos kukloforias: ";
            cin >> plate;

            cout << "\n\tTupos oximatos\t";
            cout << "\n\t1: Dikikla\t2:Autokinita\t3:Fortiga\n";
            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 << "\n\n\tTo autokinito mpike stis " << v->getEntryTime() << " apo to simeio " << v->getEntryPoint();
            cout << "\n\n\tParakalo paralabate to eisitirio";
            v = 0;
            goto mainMenu;
        case 2:

            cout << "\n\n\tEKSODOS OXIMATOS";

            cout << "\n\tArithmos kukloforias: ";
            cin >> plate;
            for(vector<Vehicle*>::iterator it = highway->begin(); it != highway->end(); it++){
                if (plate.compare((*it)->getPlate()) == 0){
                    do {
                        cout << "\n\tSe 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 << "\n\tkostos ana klm: " << km;
                    cout << "\n\n\tSINOLIKO POSO: " << (*it)->totalFee();
                    cout << "\n\n\tH apodeiksi einai ston ektupoti";
                }
                else {
                    cout << "\n\nTO OXIMA DEN YPARXEI!" << endl;
                    break;
                }
            }
            goto mainMenu;
        default:
            cout << "\n\n\tLATHOS EPILOGI! Dokimaste ksana.\n\n";
            goto mainMenu;
        }
    } while (menuSelection != 0);
    return 0;
}

【问题讨论】:

  • highway 作为指针在这里完全没用,甚至没有清理。
  • 如果我删除了程序无法编译的任何内容,为什么它没有用?
  • 如果你把它改成不是指针,你必须把所有的用途都改成匹配,否则它当然不会编译。这很糟糕,因为它是在下一行初始化的,之后没有修改,而且它永远不会为空。指针所做的只是在普通向量对象上引入丑陋的语法,并需要丑陋的手动代码来释放内存,代码示例似乎已经忘记了这一点。如果确实需要动态分配,可以通过智能指针来解决手动清理的问题,但由于第一点,动态分配显然在这种情况下没有用。
  • 您是在问这些星星是什么意思,还是为什么要这样设计代码?
  • 那是非常糟糕的代码,也是学习 C++ 编程的一个特别糟糕的例子。您可能想选择一个decent book 并开始研究它,而不是使用这样的低质量示例。

标签: c++ vector


【解决方案1】:

我不明白为什么向量上有一个指针 *hightway

因为它是一个指向vector&lt;Vehicle *&gt; 的指针,在highway = new vector&lt;Vehicle*&gt;; 行中,您会看到它是用指向new 分配的内存的指针初始化的。

为什么向量中有一颗星?

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

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

vector<Vehicle *>  highway;

以后使用highway.push_back代替highway-&gt;push_back

【讨论】:

  • 如果我移除'vector *highway;'中的星星我在下一行 'highway = new vector;' 中收到“与操作员不匹配”错误
  • @Marinos ater 那改变你不再需要那条线了
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 2018-05-27
相关资源
最近更新 更多