命名空间
变体
操作

std::dec, std::hex, std::oct

来自 cppreference.cn
< cpp‎ | io‎ | manip
 
 
 
输入/输出操纵器
浮点格式化
整数格式化
dechexoct
布尔格式化
字段宽度和填充控制
其他格式化
空白字符处理
输出刷新
(C++20)  

状态标志操作
时间与金钱 I/O
(C++11)
(C++11)
(C++11)
(C++11)
带引号的操纵器
(C++14)
 
定义于头文件 <ios>
(1)
(2)
(3)

修改整数 I/O 的默认数字基数。

1) 将流 strbasefield 设置为 dec,如同通过调用 str.setf(std::ios_base::dec, std::ios_base::basefield)
2) 将流 strbasefield 设置为 hex,如同通过调用 str.setf(std::ios_base::hex, std::ios_base::basefield)
3) 将流 strbasefield 设置为 oct,如同通过调用 str.setf(std::ios_base::oct, std::ios_base::basefield)

这是一个 I/O 操纵符。它可以用于类型为 std::basic_ostream 的任何 out,如表达式 out << std::hex;或者用于类型为 std::basic_istream 的任何 in,如表达式 in >> std::hex

目录

[编辑] 参数

str - I/O 流的引用

[编辑] 返回值

str(操作后的流的引用)。

[编辑] 示例

#include <bitset>
#include <iostream>
#include <sstream>
 
int main()
{
    std::cout << "The number 42 in octal:   " << std::oct << 42 << '\n'
              << "The number 42 in decimal: " << std::dec << 42 << '\n'
              << "The number 42 in hex:     " << std::hex << 42 << '\n';
    int n;
    std::istringstream("2A") >> std::hex >> n;
    std::cout << std::dec << "Parsing \"2A\" as hex gives " << n << '\n';
    // the output base is sticky until changed
    std::cout << std::hex << "42 as hex gives " << 42
        << " and 21 as hex gives " << 21 << '\n';
 
    // Note: there is no I/O manipulator that sets up a stream to print out
    // numbers in binary format (e.g. bin). If binary output is necessary
    // the std::bitset trick can be used:
    std::cout << "The number 42 in binary:  " << std::bitset<8>{42} << '\n';
}

输出

The number 42 in octal:   52
The number 42 in decimal: 42
The number 42 in hex:     2a
Parsing "2A" as hex gives 42
42 as hex gives 2a and 21 as hex gives 15
The number 42 in binary:  00101010

[编辑] 参阅

更改整数 I/O 所用的基数
(函数) [编辑]
控制是否使用前缀指示数字基数
(函数) [编辑]