std::showbase, std::noshowbase
来自 cppreference.cn
定义于头文件 <ios> |
||
std::ios_base& showbase( std::ios_base& str ); |
(1) | |
std::ios_base& noshowbase( std::ios_base& str ); |
(2) | |
这是一个 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 affects the output of octals and hexadecimals std::cout << std::hex << "showbase: " << std::showbase << 42 << '\n' << "noshowbase: " << std::noshowbase << 42 << '\n'; // and both input and output of monetary values std::locale::global(std::locale("en_US.UTF8")); long double val = 0; std::istringstream("3.14") >> std::showbase >> std::get_money(val); std::cout << "With showbase, parsing 3.14 as money gives " << val << '\n'; std::istringstream("3.14") >> std::noshowbase >> std::get_money(val); std::cout << "Without showbase, parsing 3.14 as money gives " << val << '\n'; }
输出
showbase: 0x2a noshowbase: 2a With showbase, parsing 3.14 as money gives 0 Without showbase, parsing 3.14 as money gives 314
[编辑] 参见
清除指定的 ios_base 标志 (函数) | |
设置指定的 ios_base 标志(函数) |