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
在构造和检查哨兵对象之后,如果value
是short
或int
,然后把它抛给unsigned short
或unsigned int
如果ios_base::flags() & ios_base::basefield
是ios_base::oct
或ios_base::hex
.在此之后,向long
在任何情况下和输出,如%283%29。如果value
是unsigned short
或unsigned 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的指针都没有重载,除了%281
0-1
2%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) |
put | inserts a character (public member function) |
write | inserts 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。