真的,"fixed" I/O 操纵器的反面是什么?
Really, what's the opposite of "fixed" I/O manipulator?
这可能是这个问题的重复,但我觉得它实际上没有正确回答。观察:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
float p = 1.00;
cout << showpoint << setprecision(3) << p << endl;
}
输出:1.00
现在,如果我们将该行更改为:
cout << fixed << showpoint << setprecision(3) << p << endl;
我们得到:1.000
如果我们使用固定的"相反",我们会得到完全不同的东西:
cout << scientific << showpoint << setprecision(3) << p << endl;
输出:1.000e+00
设置fixed
后如何返回到第一个版本的行为?
浮点的格式规范是位掩码调用std::ios_base::floatfield
。在 C++03 中,它有两个命名设置(std::ios_base::fixed
和 std::ios_base::scientific
)。默认设置是不设置这些标志。这可以实现,例如,使用
stream.setf(std::ios_base::fmtflags(), std::ios_base::floatfield);
或
stream.unsetf(std::ios_base::floatfield);
(字段类型为 std::ios_base::fmtflags
)。
对于当前C++,还std::ios_base::hexfloat
并且添加了两个机械手,特别是清除std::ios_base::floatfield
的std::defaultfloat()
:
stream << std::defaultfloat;
我认为
答案是std::defaultfloat
.但是,这仅在 C++11 中可用。请参阅 http://en.cppreference.com/w/cpp/io/manip/fixed。
在 C++11 之前,您可以清除 fixed
标志,但不能使用操纵器:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float p = 1.00;
cout << showpoint << fixed << setprecision(3) << p << endl;
// change back to default:
cout.setf(0, ios::fixed);
cout << showpoint << setprecision(3) << p << endl;
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 在 c++ 中,没有 **std::fixed** 的 **std::setprecision()** 的作用是什么?
- 真的,"fixed" I/O 操纵器的反面是什么?