C++ 为什么这段代码会不时产生不同的输出?

C++ 为什么这段代码会不时产生不同的输出?,c++,C++,编译器: ➜ ~ /usr/bin/c++ --version Apple clang version 11.0.0 (clang-1100.0.33.8) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin 代码: 另外,很抱歉,可能问题不正确,我是初学者。对于初学者,您的常规cout打印转到stdout,但“终止于…”消

编译器:

➜ ~ /usr/bin/c++ --version
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
代码:


另外,很抱歉,可能问题不正确,我是初学者。

对于初学者,您的常规
cout
打印转到
stdout
,但“终止于…”消息转到
stderr
。 您观察到的情况源于这样一个事实,即您的终端(或IDE)正在读取这两个输出通道,并将它们显示在一个位置。 根据终端/IDE用于合并这两个流的算法,可能会在您注意到的地方出现
stderr
消息

如果您将流指向同一个文件描述符,那么您的操作系统将负责合并。按如下方式运行程序:

./my_program 2>&1

现在,您的终端将按顺序显示消息,而不会出现意外重新排序的情况。

您是否在询问输出中的行顺序为何发生变化?猜测一下:“终止于…”消息将打印到
stderr
,而“异常消息”打印则转到
stdout
。如果您将一个重定向到另一个,如
/my_program 2>&1
,结果应该是确定的。@LightnessRaceswithMonica现在怎么办?@Botje建议将注释作为常规答案向下移动。哦。我所看到的是一排盒子,里面有“2B63”(我现在向上看,是一个指向下方的unicode箭头)。这是有道理的:)
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Exception message.
Exception message.
Exception message.


Exception message.
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Exception message.
Exception message.


Exception message.
Exception message.
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Exception message.
./my_program 2>&1