std::showbase, std::noshowbase
来自cppreference.com
| 在标头 <ios> 定义
|
||
| |
(1) | |
| |
(2) | |
1) 如同以调用
str.setf(std::ios_base::showbase) 启用流 str 中的 showbase 标志。2) 如同以调用
str.unsetf(std::ios_base::showbase) 禁用流 str 中的 showbase 标志。这是 I/O 操纵符,可用如 out << std::showbase 的表达式对任何 std::basic_ostream 类型 out 的,或用如 in >> std::showbase 的表达式对任何 std::basic_istream 类型的 in 调用。
showbase 标志影响整数输出(见 std::num_put::put)、货币输入(见 std::money_get::get)和货币输出(见 std::money_put::put)的行为。
参数
| str | - | 到 I/O 流的引用 |
返回值
str(到操纵后的流的引用)。
注解
按 std::num_put::put 中指定,整数输出中的 showbase 标志表现类似 std::printf 中的 # 格式指定符,这表示输出值为零时不添加数值底前缀。
示例
运行此代码
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
int main()
{
// showbase 影响八进制和十六进制的输出
std::cout << std::hex
<< "showbase: " << std::showbase << 42 << '\n'
<< "noshowbase: " << std::noshowbase << 42 << '\n';
// 及货币值的输入和输出
std::locale::global(std::locale("en_US.UTF8"));
long double val = 0;
std::istringstream("3.14") >> std::showbase >> std::get_money(val);
std::cout << "用 showbase,解析 3.14 为货币得到 " << val << '\n';
std::istringstream("3.14") >> std::noshowbase >> std::get_money(val);
std::cout << "不用 showbase,解析 3.14 为货币得到 " << val << '\n';
}
输出:
showbase: 0x2a
noshowbase: 2a
用 showbase,解析 3.14 为货币得到 0
不用 showbase,解析 3.14 为货币得到 314
参阅
| 清除指定的 ios_base 标志 (函数) | |
| 设置指定的 ios_base 标志 (函数) |