为什么C ++中的setiosflags似乎没有设置指定的标志? [英] Why does setiosflags in C++ appear not to set the specified flag?

查看:41
本文介绍了为什么C ++中的setiosflags似乎没有设置指定的标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int x = 44;
    cout << setiosflags(ios::oct) << x;
}

为什么我得到的输出是(十进制)44而不是(八进制)54?我该如何更改?

Why am I getting the output as (decimal) 44 rather than (octal) 54? How can I change this?

推荐答案

请参阅

Refer to the documentation, you should clear all format flags first

在表达式中使用<<resetiosflags(mask)或>>resetiosflags(mask),清除流的所有格式标志,或将其清除如掩码所指定.

When used in an expression out << resetiosflags(mask) or in >> resetiosflags(mask), clears all format flags of the stream out or in as specified by the mask.

写这样的东西

cout << std::resetiosflags(std::ios_base::dec) << setiosflags(ios_base::oct) << x;

这篇关于为什么C ++中的setiosflags似乎没有设置指定的标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆