我的程序发生了一些奇怪的事情。我目前在程序中使用很多线程,因此无法在此处粘贴所有内容。
但是,这是我的问题:
int value = 1000;
std::cout << value << std::endl;
//output: 3e8
知道为什么我的输出是3e8吗?
用什么命令将其修复以打印十进制值?
提前致谢! :)
最佳答案
在程序中的某处调用,例如:
std::cout << std::hex << value;
已经用过。要将输出恢复为普通(十进制),请使用:
std::cout << std::dec;
这是在std :: cout上输出数字的不同方式的relevent link。
另外,如下面的评论所指出,安全修改cout标志的标准方法似乎如下:
ios::fmtflags cout_flag_backup(cout.flags()); // store the current cout flags
cout.flags ( ios::hex ); // change the flags to what you want
cout.flags(cout_flag_backup); // restore cout to its original state
Link to IO base flags
如下面的评论所述,还应该指出,使用IO流时,最好在线程和流之间进行某种形式的同步,即确保没有两个线程可以使用相同的同步。一次流。
这样做可能还会集中您的流调用,这意味着将来调试诸如此类的东西会容易得多。
Heres an SO question that may help you