std::dec, std::hex, std::oct
来自cppreference.com
输入/输出库
输入/输出操纵符
浮点格式化 | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
整数格式化 | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
布尔格式化 | |||||||||||||||||||||||||||||||
域宽与填充控制 | |||||||||||||||||||||||||||||||
其他格式化 | |||||||||||||||||||||||||||||||
空白符处理 | |||||||||||||||||||||||||||||||
输出冲入 | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
状态标志操纵 | |||||||||||||||||||||||||||||||
时间与金钱 I/O | |||||||||||||||||||||||||||||||
带引号操纵符 | |||||||||||||||||||||||||||||||
(C++14)
|
在标头
<ios>
定义
|
||
std::ios_base& dec( std::ios_base& str );
|
(1) | |
std::ios_base& hex( std::ios_base& str );
|
(2) | |
std::ios_base& oct( std::ios_base& str );
|
(3) | |
修改整数 I/O 的默认数值底。
1) 如同以调用 str.setf(std::ios_base::dec, std::ios_base::basefield) 设置流 str
的 basefield
为
dec
2) 如同以调用 str.setf(std::ios_base::hex, std::ios_base::basefield) 设置流 str
的 basefield
为
hex
3) 如同以调用 str.setf(std::ios_base::oct, std::ios_base::basefield) 设置流 str
的 basefield
为
oct
这是一个 I/O 操纵符,可用如 out << std::hex 的表达式对任何 std::basic_ostream
类型的 out
或用如 in >> std::hex 的表达式对任何 std::basic_istream
类型的 in
调用。
参数
str | - | 到 I/O 流的引用 |
返回值
str
(到操纵后的流的引用)
示例
运行此代码
#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'; // 输出基底是持久的,直至更改 std::cout << std::hex << "42 as hex gives " << 42 << " and 21 as hex gives " << 21 << '\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
参阅
更改用于整数 I/O 的基数 (函数) |
|
控制是否使用前缀指示数值基数 (函数) |