c++11 标准模板(STL)(std::basic_ostream)(四)

basic_ostream类模板在C++中提供字符流的高层输出操作,支持格式化和无格式化数据插入,如整数、浮点数、布尔值和指针。插入函数包括不同类型的数值和特殊操作,如streambuf和操纵符函数。在插入过程中,可能会涉及数据转换和错误处理。
摘要由CSDN通过智能技术生成
定义于头文件 <ostream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ostream : virtual public std::basic_ios<CharT, Traits>

类模板 basic_ostream 提供字符流上的高层输出操作。受支持操作包含有格式输出(例如整数值)和无格式输出(例如生字符和字符数组)。此功能以 basic_streambuf 类所提供的接口实现,通过 basic_ios 基类访问。典型的实现中, basic_ostream 无非继承的数据成员。

有格式输出

插入带格式数据

std::basic_ostream<CharT,Traits>::operator<<

basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );

(1)

basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );

(2)

basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );

(3)

basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );

(4)(C++11 起)
basic_ostream& operator<<( float value );

basic_ostream& operator<<( double value );

basic_ostream& operator<<( long double value );
(5)

basic_ostream& operator<<( bool value );

(6)

basic_ostream& operator<<( const void* value );

(7)

basic_ostream& operator<<( std::nullptr_t );

(8)(C++17 起)

basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);

(9)

basic_ostream& operator<<(
    std::ios_base& (*func)(std::ios_base&) );

(10)

basic_ostream& operator<<(
    std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );

(11)

basic_ostream& operator<<(
    std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );

(12)

插入数据到流。

1-2) 表现同有格式输出函数 (FormattedOutputFunction) 。在构造并检查 sentry 对象后,若 value 为 short 或 int ,则将它转型为 unsigned short 或 unsigned int ,若 ios_base::flags() & ios_base::basefield 为 ios_base::oct 或 ios_base::hex 。之后,不论任何情况,将它转型为 long 并按 (3) 中方式输出。若 value 是 unsigned short 或 unsigned int ,则将它转型为 unsigned long 并按 (3) 中方式输出。

3-7) 表现同有格式输出函数 (FormattedOutputFunction) 。在构造并检查 sentry 对象后,通过调用 num_put::put() 插入整数、浮点数、布尔或通用指针值。若在输出中遇到文件尾( put().failed() == true ),则设置 ios::badbit 。

8) 如同用 *this << s 输出实现定义的字符串,其中 s 是空终止字符类型字符串。

9) 表现同无格式输出函数 (UnformattedOutputFunction) 。在构造并检查 sentry 对象后,检查 sb 是否空指针。若它是,则执行 setstate(badbit) 并退出。否则,从 sb 所控制的输入序列释出字符,并将它们插入到 *this ,直至满足下列条件之一为止:

  • 输入序列上出现文件尾;
  • 插入输出序列失败(该情况下不会释出要被插入的字符);
  • 发生异常(该情况下异常被捕捉)。

若没有插入字符,则执行 setstate(failbit) 。若在释放中抛出异常,则设置 failbit ,而且若 failbit 设置于 exceptions() ,则重抛异常。

10-12) 调用 func(*this) 。这些重载用于实现输出 I/O 操纵符,例如 std::endl 。

参数

value-要插入的整数、浮点、布尔或指针值
func-要调用的函数
sb-指向要自之读取数据的 streambuffer 的指针

返回值

1-11) *this

12) func(*this)

注意

无对指向非静态数据成员指针、指向 volatile 指针或函数指针(除了拥有 (10-12) 重载所接受之签名者)的重载。试图输出这种对象会引发到 bool 的隐式转换,并对于任何非空指针值打印值 1 (除非设置了 boolalpha ,该情况下打印 true )。

字符与字符串参数(例如拥有 char 类型或 const char* 类型者)以 operator<< 的非成员重载处理。试图用成员函数调用语法(例如 std::cout.operator<<('c'); )会调用重载 (2-4) 之一,并输出数字值。试图用成员函数调用语法输出字符串会调用重载 (7) ,并替而打印指针值。

调用示例

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    std::istringstream input(" \"Some text.\" ");
    volatile int n = 42;
    double f = 3.14;
    bool b = true;
    std::cout << n   // int 重载
              << ' ' // 非成员重载
              << std::boolalpha << b // bool 重载
              << " " // non-member overload
              << std::fixed << f // double 重载
              << input.rdbuf() // streambuf 重载
              << &n // bool 重载: volatile int* 不转换成 const void*
              << std::endl; // 函数重载
}
输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值