c++ - cout的异常行为
问题描述
该程序运行良好并在使用 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 语句不起作用。
解决方案
推荐阅读
- android - 移除 Item RecyclerView 时添加自定义动画
- visual-studio-code - SetAscending Business Central 销售订单列表
- node.js - 使用 AWS Lambda 删除 Cognito 用户
- r - R:在多个变量上计算逐行 lm()
- angular - 尽管在现有模块中导入了 CommonModule,但 ngFor 仍无法正常工作
- node.js - NodeJS:从多个承诺读取和写入共享对象
- spring - (反)使用 Jackson 序列化 Spring Boot ConfigurationProperties
- python - 有没有办法从 numpy 数组中随机减去一个百分比?
- python - 是否有与 Java 的 Calendar.DAY_OF_WEEK_IN_MONTH 等效的 Python 版本?
- neo4j - 如何删除密码中的大量关系?