gpt4 book ai didi

c++ - 为什么 C++ 中的 setiosflags 出现没有设置指定的标志?

转载 作者:行者123 更新时间:2023-11-30 03:35:22 24 4
gpt4 key购买 nike

#include <iostream>
#include <iomanip>

using namespace std;

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

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

最佳答案

引用documentation , 你应该先清除所有格式标志

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++ - 为什么 C++ 中的 setiosflags 出现没有设置指定的标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41340933/

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