gpt4 book ai didi

c++ - 真的, "fixed"I/O 操纵器的反面是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:28 24 4
gpt4 key购买 nike

这可能是 this question 的拷贝,但我不认为它实际上得到了正确回答。观察:

#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

如果我们使用 fixed 的“相反”,我们会得到完全不同的东西:

  cout << scientific << showpoint << setprecision(3) << p << endl;

输出:1.000e+00

在设置了fixed 后,如何返回到第一个版本的行为?

最佳答案

float 的格式规范是位掩码调用 std::ios_base::floatfield。在 C++03 中,它有两个命名设置(std::ios_base::fixedstd::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::defaultfloat() 清除了 std::ios_base::floatfield:

stream << std::defaultfloat;

关于c++ - 真的, "fixed"I/O 操纵器的反面是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com