C++标准模板(STL)- 输入/输出操纵符-(std::uppercase, std::nouppercase,std::unitbuf, std::nounitbuf)

操纵符是令代码能以 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::uppercase, 
std::nouppercase

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

(1)

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

(2)

启用浮点和十六进制整数输出中大写字符的使用。在输入时无效果。

1) 如同用调用 str.setf(std::ios_base::uppercase) 启用流 str 中的 uppercase 标志

2) 如同用调用 str.unsetf(std::ios_base::uppercase) 禁用流 str 中的 uppercase 标志

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

参数

str-到 I/O 流的引用

返回值

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

调用示例

#include <iostream>

int main()
{
    std::cout << std::hex << std::showbase
              << "0x2a with uppercase: "
              << std::uppercase << 0x2a << std::endl
              << "0x2a with nouppercase: "
              << std::nouppercase << 0x2a << std::endl
              << "1e-10 with uppercase: "
              << std::uppercase << 1e-10 << std::endl
              << "1e-10 with nouppercase: "
              << std::nouppercase << 1e-10 << std::endl;

    return 0;
}

输出

控制是否每次操作后冲洗输出

std::unitbuf, 
std::nounitbuf

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

(1)

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

(2)

启用或禁用任何输出操作后的自动冲入。在输入时无效果。

1) 如同用调用 str.setf(std::ios_base::unitbuf) 启用流 str 中的 unitbuf 标志

2) 如同用调用 str.unsetf(std::ios_base::unitbuf) 禁用流 str 中的 unitbuf 标志

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

注解

在 std::basic_ostream::sentry 对象的析构函数中进行冲入,若 str.flags() & std::ios_base::unitbuf 为 true 则析构函数调用 str.rdbuf()->pubsync() 。

标准输出对象 std::cerr 及 std::wcerr 默认已设置其 unitbuf 位。

参数

str-到 I/O 流的引用

返回值

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

调用示例

#include <iostream>
#include <chrono>
#include <iostream>

template<typename Diff>
void log_progress(Diff d)
{
    std::cout << "..("
              << std::chrono::duration_cast<std::chrono::milliseconds>(d).count()
              << " ms)..";
    std::cout << std::endl;
}

int main()
{
    volatile int sink = 0;
    std::cout << std::unitbuf; // 启用自动冲入

    auto t1 = std::chrono::high_resolution_clock::now();
    for (int j = 0; j < 5; ++j)
    {
        for (int n = 0; n < 10000; ++n)
        {
            for (int m = 0; m < 20000; ++m)
            {
                sink += m * n;    // 做一些工作
            }
        }

        auto now = std::chrono::high_resolution_clock::now();
        log_progress(now - t1);
    }
    std::cout << std::endl;

    return 0;
}

输出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值