对于我的学习作业,我不得不仅使用智能指针进行定向加权图形实现。
TransportationSystem类(图表):
class TransportationSystem {
public:
TransportationSystem(std::vector<std::string> _inputFiles);
TransportationSystem(std::vector<std::string> _inputFiles, std::string _outputFile);
TransportationSystem(std::vector<std::string> _inputFiles, std::string _outputFiles, std::string _configFile);
void loadAll();
void loadConfig();
void loadFile(std::string fileName) throw();
void printAllPaths();
void printOutboundPath(std::string rootStationName);
std::vector<Station>& getStations() { return stations;}
class CouldNotOpenFileException {
};
private:
std::vector<std::string> inputFiles;
std::string outputFile;
std::string configFile;
std::vector<Station> stations;
int busTransitionTime;
int tramTransitionTime;
int sprinterTransitionTime;
int railTransitionTime;
bool defaultValues;
int csTransitionTime;
int sTransitionTime;
int icTransitionTime;
int getWaitingTimeByStationType(std::string _stationType);
int getTransitionTimeByTransportationType(std::string _transportationType);
void searchAndPrintPathByTransportationType(std::string transportationType, std::string rootName);
void updateValue(std::string key, int value);
};
Station class(Vertex):
class Station {
public:
Station(std::string _stationName, std::string _stationType, int _changingTime)
:stationName(_stationName), stationType(_stationType), changingTime(_changingTime) {}
void addRoad(Road& _road) {roads.push_back(_road);}
std::vector<Road>& getRoads() { return roads;}
std::string getStationName() const { return stationName;}
bool operator==(const Station & obj2) const;
private:
std::string stationName;
std::string stationType;
std::vector<Road> roads;
int changingTime;
};
道路类(Edge):
class Road {
public:
Road() {}
Road(std::string _transportationType, std::string _source, std::string _destination, int _distance,
int _transportationWaitingTime);
Road(std::string _transportationType, std::string _source, std::string _destination, int _distance);
void setDefaultValues(std::string transportationType, std::string sourceNode, std::string targetNode,
int distance);
void setNonDefaultValues(std::string transportationType, std::string sourceNode, std::string targetNode,
int distance, int waitingTime);
std::shared_ptr<Station*> nextStation;
std::string getSourceNode() const { return source;}
std::string getDestinationNode() const { return destination;}
std::string getTransportationType() const { return transportationType;}
int getDistance() { return distance;}
bool operator==(const Road & obj2) const;
private:
std::string transportationType;
int distance;
int transportationWaitingTime;
std::string source;
std::string destination;
};
因此,该站将成为图形的顶点,道路将成为边缘。
正如你在Station类中看到的那样,我拿着一个道路(边缘)矢量,每条道路都有一个智能指针(称为nextStation),以便指向相应的下一站。
在这个函数中,我加载了图中的所有连接:
void TransportationSystem::loadAll() {
for (int i = 0; i < inputFiles.size(); ++i) {
ifstream infile(inputFiles[i]);
string sourceNode;
string targetNode;
int distance;
string transportationType = getTransportationTypeByFileName(inputFiles[i]);
string sourceStationType;
string targetStationType;
while (infile >> sourceNode >> targetNode >> distance) {
// Making a new road
Road r;
if (defaultValues == true)
r.setDefaultValues(transportationType, sourceNode, targetNode, distance);
else
r.setNonDefaultValues(transportationType, sourceNode, targetNode, distance, getTransitionTimeByTransportationType(transportationType));
/* Here starts the trial scope */
sourceStationType = getStationTypeBySourceNode(sourceNode);
targetStationType = getStationTypeBySourceNode(targetNode);
vector<Station>::iterator sourceNodeIt = find(stations.begin(), stations.end(), Station(sourceNode, sourceStationType, getWaitingTimeByStationType(sourceStationType)));
vector<Station>::iterator targetNodeIt = find(stations.begin(), stations.end(), Station(targetNode, targetStationType, getWaitingTimeByStationType(targetStationType)));
if (sourceNodeIt != stations.end() && targetNodeIt != stations.end()) {
// Both Source node and Target node are on the stations vector
// 1 - We have to point with the new road's pointer to the target node
// 2 - insert the road to the source roads vector
r.nextStation = make_shared<Station*>(&(*targetNodeIt));
(*sourceNodeIt).addRoad(r);
} else if (sourceNodeIt != stations.end() && targetNodeIt == stations.end()) {
// Source node exists on the stations vector but target node doesnt exist in the station vector
// 1 - So we create a new station for the target node
// 2 - We point with the new road to the target node we just create
// 3 - We insert the new road to the existing source node
// 4 - We push the new target station to the station vector
Station targetStation(targetNode, targetStationType, getWaitingTimeByStationType(targetStationType));
r.nextStation = make_shared<Station*>(&targetStation);
(*sourceNodeIt).addRoad(r);
stations.push_back(targetStation);
//sourceNodeIt = find(currentStations.begin(), currentStations.end(), Station(sourceNode, sourceStationType, getWaitingTimeByStationType(sourceStationType)));
} else if (sourceNodeIt == stations.end() && targetNodeIt != stations.end()) {
// Source node doesnt exist in the station vector but target vector exists
// 1 - We have to point with the new road's pointer to the target node
// 2 - We create a new station for the source node
// 3 - We insert the new road to the new station we've just create
// 4 - We push the new source station to the stations vector
r.nextStation = make_shared<Station*>(&(*targetNodeIt));
Station sourceStation(sourceNode, sourceStationType, getWaitingTimeByStationType(sourceStationType));
sourceStation.addRoad(r);
stations.push_back(sourceStation);
} else {
// Neither source node or vector doesn't exist in the station vector
// 1 - We create a new station for the source node
// 2 - We create a new station for the target node
// 3 - We point with the new road to the target node
// 4 - We insert the new road to the source node
// 5 - We push both to the stations vector
Station sourceStation(sourceNode, sourceStationType, getWaitingTimeByStationType(sourceStationType));
Station targetStation(targetNode, targetStationType, getWaitingTimeByStationType(targetStationType));
r.nextStation = make_shared<Station*>(&targetStation);
sourceStation.addRoad(r);
stations.push_back(sourceStation);
stations.push_back(targetStation);
}
}
}
}
通过尝试这样的事情:
r.nextStation = make_shared(&amp;(* targetNodeIt));
我希望指向我的工作站矢量中的实际工作站,但是在调试时我可以看到,如果我在工作站矢量上的特定工作站上更改某些内容,它就不会改变我之前指出的对象
请帮忙!