c++ - 在 setiosflags 函数中使用十六进制标志

标签 c++ iomanip

int a=60;
cout<<setiosflags(ios::hex|ios::showbase|ios::uppercase);
cout<<a<<endl;

上面的代码不起作用,但如果我使用
cout<<hex

进而
cout<<setiosflags(ios::showbase|ios::uppercase)

然后它正在工作

为什么?以及我怎么知道哪一个可以在 setiosflags() 中使用?

最佳答案

您需要调用 resetiosflags 在您调用 setiosflags 之前.原因是 setiosflags(ios::hex|ios::showbase|ios::uppercase)只需将这些标志附加到流中,就像调用 setf这会在流中给出冲突的标志。使用

std::cout << std::resetiosflags(std::ios_base::dec)
          << std::setiosflags(std::ios::hex|std::ios::showbase|std::ios::uppercase)
          << a << endl;

将使其显示a正确。

关于c++ - 在 setiosflags 函数中使用十六进制标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62069504/

相关文章:

c++ - 使用 iomanip 指令

c++ - 查找输出流的当前基

c++ - 错误 : ‘unique_ptr’ is not a member of ‘std’

c++ - 为什么即使 T 的 value_type 是 const,std::is_const::value 也是 'false'?

c++ - 关于为模板提供默认类型的问题

c++ - 为什么我不能更改新创建文件的 'last write time'?

c++ - 如何键入用于 const 对象的自定义 io 操纵器?

c++ - wordwap 函数修复以保留单词之间的空格

c++ - C++中 float 的全精度显示?

c++ - 对 C++ 中的 std::get_money 和 std::put_money 的混淆