使用Codeblocks IDE(使用g ++编译器)运行时,程序运行正常并产生预期的输出。但是,当使用g ++编译器在linux终端上运行时,cout语句的行为异常,并且不会打印一半的语句。我只粘贴了整个代码的打印功能。
void print_result(int dist[], int previous[], int source, int destination)
{
int temp = destination, n=1;
answer[0] = destination;
cout<<"\n The shortest distance from the "<<names[source]<<" to "<<names[destination]<<" is: "<<dist[destination]<<endl;
cout<<"The path from the "<<names[source]<<" to "<<names[destination]<<" is:"<<endl;
while(temp != source) // the previous of the node is recorded until the source is reached
{
answer[n] = previous[temp];//the previous of all the nodes from the destination to the source is recorded
temp = previous[temp];
n = n+1;
}
for(int i=n-1; i>=0; i--) // since the previous loop was run n-1 times, it is clear that there are n-1 vertices between the source and the destination
{
cout<<" "<<nodes[answer[i]]<<" ("<<names[answer[i]]<<")";//the path is printed from source to destination
if(i!=0) // this condition is just to make sure that the arrow is not printed for the last element
{
cout<<" -->";
}
}
}
我在代码块IDE中获得的输出是
Enter the source node 44
Enter the destination node 82
The shortest distance from the Mathews Arena to East village is: 811
The path from the Mathews Arena to East village is:
44 ( Mathews Arena) --> 45 ( Gainsborough Parking Garage) --> 47 ( Cullinane hall) --> 82 ( East village)
但是我在linux终端中得到的输出是
Enter the source node 44
Enter the destination node 82
is: 811t villagetance from the Mathews Arena
is: East village Mathews Arena
无法理解为什么我的cout语句不起作用。