在c ++中关闭setf?

bff*_*f02 4 c++ setf

我使用setf来显示输出中的小数位.

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
Run Code Online (Sandbox Code Playgroud)

但是,当我将上面的代码放在输出之前时,其他输出也会受到影响.有什么办法我只能用一个输出来设置setf吗?喜欢把它关掉?

非常感谢!

Lig*_*ica 6

setf 返回原始标志值,因此您可以简单地存储它,然后在完成后将其放回原处.

同样如此precision.

所以:

// Change flags & precision (storing original values)
const auto original_flags     = std::cout.setf(std::ios::fixed | std::ios::showpoint);
const auto original_precision = std::cout.precision(2);

// Do your I/O
std::cout << someValue;

// Reset the precision, and affected flags, to their original values
std::cout.setf(original_flags, std::ios::fixed | std::ios::showpoint);
std::cout.precision(original_precision);
Run Code Online (Sandbox Code Playgroud)

阅读一些文档.