STD :: Ostream忽略了通过setf()在基础上设置的十六进制标志
std::ostream ignores hex flag set on the underlying via setf()
以下C 代码令人惊讶地产生小数输出,显然忽略了对setf()
的调用和打印true 42
。使用std::setiosflags()
给出相同的结果。但是,使用std::cout << std::hex
确实给出了预期的输出true 0x2a
,因此std::ios::showbase
和std::ios::boolalpha
得到了尊重。
我已经在Ubuntu上测试了G 5.4和CentOS上的G 7.2.1。我在这里缺少什么?
#include <sstream>
#include <iostream>
#include <iomanip>
#include <iterator>
int main()
{
std::cout.setf(std::ios::hex | std::ios::showbase | std::ios::boolalpha);
// Uncommenting the line below doesn't make a difference.
//std::cout << std::setiosflags(std::ios::hex | std::ios::showbase | std::ios::boolalpha);
// Uncommenting this line does give the desired hex output.
//std::cout << std::hex;
int m = 42;
std::cout << true << ' ' << m << std::endl;
return 0;
}
setf
的这种变体仅添加 flags,但您需要清除基本字段。
因此,您需要使用口罩的过载:
std::cout.setf(std::ios::hex | std::ios::showbase | std::ios::boolalpha,
std::ios_base::basefield | std::ios::showbase | std::ios::boolalpha);
输出:
true 0x2a
live demo
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 无法在 HEVC 解码器上设置输出类型 IMFTransform
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 代码在 msvcrt .dll函数上设置断点失败
- 如何在引用上设置数据断点
- 如何让我的程序在输入验证的基础上读取素数
- Qt如何在QRect上设置焦点
- 如何使用Cmake在Linux上设置MKL
- C++ Cmake没有在Travis CI服务器上设置编译器语言标准
- 在Windows上设置CMake和vcpkg时出错"无法打开包含文件"
- 如何在枚举上设置范围?
- 如何在Visual Studio上设置libcurl
- 在通过 P/Invoke 获取的 C++ 结构上设置 C# 回调
- 在Visual "Microsoft studio 2019"上设置OpenCV 4.1.1时遇到问题?(非法指令。
- 如何在 WinDBG 预览版上设置源服务器?
- 添加 swig pythoncode 以在 Python 对象上设置自己的标志
- 如何在窗口上设置使用 CreateProcess 创建的新进程的主线程的堆栈大小?
- 如何在 osg::Geometry 上设置光泽度,而无需在 OpenSceneGraph 中设置材质
- (重新定义错误)从C 中的一个基础上创建多个Inherting类
- STD :: Ostream忽略了通过setf()在基础上设置的十六进制标志