CppDS.com

C++ 98 11 14 17 20 手册

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

来自cppreference.com
< cpp‎ | io‎ | basic ostream
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 对象后,若 valueshortint ,则将它转型为 unsigned shortunsigned int ,若 ios_base::flags() & ios_base::basefieldios_base::octios_base::hex 。之后,不论任何情况,将它转型为 long 并按 (3) 中方式输出。若 valueunsigned shortunsigned 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; // 函数重载
}

输出:

42 true 3.140000 "Some text." true

参阅

插入字符数据
(函数)
进行 string 的流 I/O
(函数模板)
进行 bitset 的流输入与输出
(函数)
序列化与反序列化复数
(函数模板)
执行伪随机数引擎的流输入和输出
(函数)
执行伪随机数分布的流输入和输出
(函数模板)
插入字符
(公开成员函数)
插入字符块
(公开成员函数)
(C++17)
转换整数或浮点值到字符序列
(函数)
关闭