这个特定的载体是如何工作的
How is this specific vector working?
您好,我得到了这段工作正常的代码,但我无法理解其中的某些部分,如果有人有空闲时间,我真的可以使用一些帮助。我不明白为什么矢量上有指针 *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
.
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 链表c++插入,所有情况都已检查,但没有任何工作
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?