C++标准模板(STL)- 输入/输出操纵符-(std::boolalpha, std::noboolalpha,std::showbase, std::noshowbase)

操纵符是令代码能以 operator<< 或 operator>> 控制输入/输出流的帮助函数。

不以参数调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex; )实现为接受到流的引用为其唯一参数的函数。 basic_ostream::operator<< 和 basic_istream::operator>> 的特别重载版本接受指向这些函数的指针。这些函数(或函数模板的实例化)是标准库中仅有的可取址函数。 (C++20 起)

以参数调用的操纵符(例如 std::cout << std::setw(10); )实现为返回未指定类型对象的函数。这些操纵符定义其自身的进行请求操作的 operator<<operator>>

在布尔值的文本和数值表示间切换

std::boolalpha, 
std::noboolalpha

std::ios_base& boolalpha( std::ios_base& str );

(1)

std::ios_base& noboolalpha( std::ios_base& str );

(2)

1) 启用流 str 中的 boolalpha 标志,如同通过调用 str.setf(std::ios_base::boolalpha) 。

2) 禁用流 str 中的 boolalpha 标志,如同通过调用 str.unsetf(std::ios_base::boolalpha) 。

std::boolalpha 是 I/O 操纵符,故可用如 out << std::boolalpha 的表达式对任何 std::basic_ostream 类型的 out ,或用如 in >> std::boolalpha 的表达式对任何 std::basic_istream 类型的 in 调用。

参数

str-到 I/O 流的引用

返回值

str (到操纵后的流的引用)

 调用示例

#include <sstream>
#include <locale>
#include <iostream>

int main()
{
    // boolalpha 输出
    std::cout << std::boolalpha
              << "boolalpha true: " << true << std::endl
              << "boolalpha false: " << false << std::endl;
    std::cout << std::noboolalpha
              << "noboolalpha true: " << true << std::endl
              << "noboolalpha false: " << false << std::endl;

    // boolalpha 分析
    bool b1 = false;
    bool b2 = false;
    std::istringstream is("true false");
    is >> std::boolalpha >> b1 >> b2;
    std::cout << '\"' << is.str() << "\" parsed as "
              << b1 << ' ' << b2 << std::endl;

    return 0;
}

输出

控制是否使用前缀指示数值基数

std::showbase, 
std::noshowbase

std::ios_base& showbase( std::ios_base& str );

(1)

std::ios_base& noshowbase( std::ios_base& str );

(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 <sstream>
#include <locale>
#include <iostream>
#include <iomanip>

int main()
{
    // showbase 影响八进制和十六进制的输出
    std::cout << std::hex
              << "showbase: " << std::showbase << 42 << std::endl
              << "noshowbase: " << std::noshowbase << 42 << std::endl;

    // 及货币值的输入和输出
    std::locale::global(std::locale("POSIX"));
    long double val = 0;
    std::istringstream is("3.14");
    is >> std::showbase >> std::get_money(val);
    std::cout << "With showbase, parsing 3.14 as money gives "
              << val << std::endl;
    is.seekg(0);
    is >> std::noshowbase >> std::get_money(val);
    std::cout << "Without showbase, parsing 3.14 as money gives "
              << val << std::endl;

    return 0;
}

输出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值