gpt4 book ai didi

c++ - 队列不添加对象 - C++ 11

转载 作者:行者123 更新时间:2023-11-30 04:42:21 24 4
gpt4 key购买 nike

我有两个自定义类,PassengerElevator 包含在 Passenger.h 头文件中:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Passenger
{
public:

explicit Passenger(int startTime, int startFloor, int endFloor, int waitTime){
this->startTime = startTime;
this->startFloor = startFloor;
this->endFloor = endFloor;
this->waitTime = waitTime;
}//end constructor

void increaseWait(int increase){
this->waitTime += increase;
}

int getWait(){
return this->waitTime;
}

int startTime, startFloor, endFloor, waitTime;

}; //end passenger


class Elevator
{
public:

explicit Elevator(string the){
status = "STOPPED";
passengerCount = 0;
name = the;
}//end constructor

int addtoQueue(Passenger person){
if (this->passengerCount < 8){
this->pickupQueue.push(person);
return 0;
}else{
cout << "Error -- full" << endl;
return 1;
} //end

} //end addtoQueue

void changePassengerCount(int change){
passengerCount += change;
}

string getStatus(){
return status;
}

void setStatus(string status){
status = status;
}

void addStop(int location){
stops.push(location);
}

int getStops(){
return stops.size();
}

string getName(){
return name;
}

int getRoute(){
return stops.front();
}

bool checkCount(){
if (passengerCount > 8){
return false;
}else{
return true;
}
}

int printCount(){
return passengerCount;
}

void printQueue(){
queue<int> copy = stops;

while(!copy.empty()){
cout << copy.front() << " ";
copy.pop();
}

cout << endl;
}

private:
string status;
queue<Passenger> pickupQueue;
int passengerCount;
queue<int> stops;
string name;

}; //end passenger

我正在将 csv 文件读入 Passenger 对象的 vector 中,然后我想做的是填充一些 将每个 Elevator 对象中的 对象与那些 Passenger 对象一起排队。但是,似乎 Elevator 类中的所有对象都没有实际更新。例如,passengerCountQueue Size 永远不会超过 1。我不明白为什么。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "Passenger.h"
using namespace std;

int time = 0;

int main(){
vector<Passenger> passengers;
Elevator elevatorOne = Elevator("elevatorOne");
Elevator elevatorTwo = Elevator("elevatorTwo");
Elevator elevatorThree = Elevator("elevatorThree");
Elevator elevatorFour = Elevator("elevatorFour");
vector<Elevator> elevators;
elevators.push_back(elevatorOne);
elevators.push_back(elevatorTwo);
elevators.push_back(elevatorThree);
elevators.push_back(elevatorFour);

// Code removed when building the vector of passengers by reading in the csv

for(unsigned int i = 0; i < passengers.size(); i++){
//cout << "Currently evaluating passenger: " << i << endl;
for (Elevator currElevator : elevators){
// If the elevator hasn't moved
// This is one of three logic checks, but the others have been removed at the request of SO users
if(currElevator.getStatus().compare("STOPPED") == 0 && currElevator.getStops() == 0 && currElevator.checkCount() == true){
// Add the passenger's end floor to the current elevator's queue
cout << "adding passenger " << i << " to elevator: " << currElevator.getName() << " because of condition 1 " << endl;
currElevator.addStop(passengers[i].endFloor);
currElevator.changePassengerCount(1);
cout << "Queue Size: " << currElevator.getStops() << endl;
cout << "Passenger Count: " << currElevator.printCount() << endl;
if(passengers[i].endFloor > currElevator.getRoute()){
currElevator.setStatus("MOVING UP");
} else{
currElevator.setStatus("MOVING DOWN");
}

break;
} //end if

else{
// If none of these conditions match, move on to the next elevator
continue;
} //end else
} //end looping through elevators
} //end looping through passengers
} //end main

我得到的结果如下:

adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 1

但希望看到:

adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 2
Passenger Count: 2
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 3
Passenger Count: 3

等等

最佳答案

为一个愚蠢的问题道歉。

替换:

for (Elevator currElevator : elevators){

与:

for (int j = 0; j < elevators.size(); j++){

所有对 currElevatorelevators[j] 的调用都解决了这个问题。

我目前不确定为什么会产生影响,但会通过进一步研究更新此答案。

更新

根据科里斯托:发生这种情况的原因是基于范围的 for 循环内的 currElevatorelevators vector 中电梯的拷贝。如果您将基于范围的 for 循环声明为 for (Elevator& currElevator : elevators) 它应该可以工作,因为现在 currElevator 是对 elevators< 中元素的引用 vector 而不是拷贝。

关于c++ - 队列不添加对象 - C++ 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com