在线文档教程
C++
输入/输出 | Input/output

std::basic_ostream::operator<<

性病:基本[医]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)(since 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)(since 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%29表现为FormattedOutputFunction在构造和检查哨兵对象之后,如果valueshortint,然后把它抛给unsigned shortunsigned int如果ios_base::flags() & ios_base::basefieldios_base::octios_base::hex.在此之后,向long在任何情况下和输出,如%283%29。如果valueunsigned shortunsigned int,向unsigned long和%283%29中的输出。

3-7%29表现为FormattedOutputFunction.在构造和检查哨兵对象之后,通过以下方式插入一个整数、浮点、布尔值或泛型指针值num_put::put()如果在输出%28期间遇到文件结束情况put().failed() == true%29,成套ios::badbit...

8%29输出一个实现定义的字符串,好像*this << s,在哪里s是以空结尾的字符类型字符串。

9%29表现为UnformattedOutputFunction在构造和检查哨兵对象之后,检查sb为空指针。如果是,则执行setstate(badbit)和出口。否则,从由sb并将它们插入*this在满足下列条件之一之前:

  • 文件结束发生在输入序列上;

  • 在输出序列中插入失败%28,在这种情况下,要插入的字符不会提取%29;

  • 异常发生在%28,在这种情况下,异常被捕获为%29。

如果没有插入字符,则执行setstate(failbit)如果在解压缩时引发异常,则设置failbit如果failbit设为exceptions()重新抛出异常。

10-12%29次电话func(*this这些过载用于实现输出I/O操作器,例如std::endl...

参数

value-integer, floating-point, boolean, or pointer value to insert
func-function to call
sb-pointer to the streambuffer to read the data from

返回值

1-11%29*this

12%29func(*this)

注记

指向非静态成员的指针、到易失性的指针或函数指针%28的指针都没有重载,除了%2810-12%29重载%29所接受的签名外。试图输出这类对象时,将调用隐式转换到bool,对于任何非空指针值,1打印%28,除非boolalpha设置为%29。

字符和字符串参数%28e。g.,类型char或const char*%29由非会员过载成operator<<试图使用成员函数调用语法%28e输出字符。g.std::cout.operator<<('c'%29将调用过载%282-4%29中的一个并输出数值.。尝试使用成员函数调用语法输出字符串将调用重载%287%29并打印指针值。

二次

#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 overload << ' ' // non-member overload << std::boolalpha << b // bool overload << " " // non-member overload << std::fixed << f // double overload << input.rdbuf() // streambuf overload << &n // bool overload: volatile int* doesn't convert to const void* << std::endl; // function overload }

二次

产出:

二次

42 true 3.140000 "Some text." true

二次

另见

operator<<(std::basic_ostream)inserts character data (function template)
operator<<operator>>performs stream I/O of strings (function template)
operator<<operator>>performs stream input and output of bitsets (function)
operator<<operator>>serializes and deserializes a complex number (function template)
operator<<operator>>performs stream input and output on pseudo-random number engine (function template)
operator<<operator>>performs stream input and output on pseudo-random number distribution (function template)
putinserts a character (public member function)
writeinserts blocks of characters (public member function)
to_chars (C++17)converts an integer or floating-point value to a character sequence (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]O流/操作员[医]LTLT