std::basic_ostream::operator<<
来自cppreference.com
< cpp | io | basic ostream
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
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 ); |
(5) | |
basic_ostream& operator<<( bool value ); |
(6) | |
basic_ostream& operator<<( const void* value ); |
(7) | |
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb); |
(8) | |
basic_ostream& operator<<( basic_ostream& st, std::ios_base& (*func)(std::ios_base&) ); |
(9) | |
数据插入到流
格式的输出功能。(1-7)版本的运营商的行为,而8)版本的数据的行为视为未格式化的输出功能。这些函数开始执行构造一个对象的类型
1-2) sentry
,刷新tie()'d的输出缓冲区,如果必要的检查流错误。项目建成后,如果哨兵对象返回false,函数返回没有尝试任何输出。如果发生错误,在输出过程中,格式化输出函数设置setstate(ios_base::failbit)。如果抛出一个异常,然后在输出的ios :: badbit((exceptions()&badbit) != 0被抑制,除非,在这种情况下,它被重新抛出异常)原文:
The (1-7) versions of the operator behave as formatted output functions, and the 8) version behaves as unformatted output function. These functions begin execution by constructing an object of type
sentry
, which flushes the tie()'d output buffers if necessary and checks the stream errors. After construction, if the sentry object returns false, the functions return without attempting any output. If an error occurs during output, formatted output functions set setstate(ios_base::failbit). If an exception is thrown during output, then ios::badbit is set (the exception is suppressed unless (exceptions()&badbit) != 0, in which case it is rethrown)如果
3-4) value
short或int,然后将其转换为unsigned short或unsigned int如果ios_base::flags() & ios_base::basefield是ios_base::oct或ios_base::hex。之后,在任何情况下,在3输出转换long)。如果value
是unsigned short或unsigned int,蒙上unsigned long和输出的3)原文:
If
value
is short or int, then casts it to unsigned short or unsigned int if ios_base::flags() & ios_base::basefield is ios_base::oct or ios_base::hex. After that, casts to long in any case and outputs as in 3). If value
is unsigned short or unsigned int, casts to unsigned long and outputs as in 3)将一个整数值,通过调用num_put::put()。如果最终文件的情况过程中遇到的输出(put().failed() == true),设置ios::badbit.
5) 原文:
Inserts an integer value by calling num_put::put(). If the end of file condition was encountered during output (put().failed() == true), sets ios::badbit.
将一个浮点值,通过调用过程中遇到的输出(num_put::put())put().failed() == true如果最终文件的情况,设置ios::badbit.
6) 原文:
Inserts a floating point value by calling num_put::put() If the end of file condition was encountered during output (put().failed() == true), sets ios::badbit.
插入bool值通过调用num_put::put()如果最终的文件条件的过程中遇到的输出(put().failed() == true),设置ios::badbit.
7) 原文:
Inserts bool value by calling num_put::put() If the end of file condition was encountered during output (put().failed() == true), sets ios::badbit.
通过调用num_put::put()如果最终的文件条件的过程中遇到的输出(put().failed() == true)插入一个通用的指针值,设置ios::badbit.
8) 原文:
Inserts a generic pointer value by calling num_put::put() If the end of file condition was encountered during output (put().failed() == true), sets ios::badbit.
将所有数据从
sb
。构建哨兵对象后,检查是否sb
是一个空指针。如果是的话,执行setstate(badbit)并退出。否则,提取的字符输入序列sb
,并将其插入到控制*this,直到满足以下条件:1原文:
Inserts all data from
sb
. After constructing the sentry object, checks if sb
is a null pointer. If it is, executes setstate(badbit) and exits. Otherwise, extracts characters from the input sequence controlled by sb
and inserts them into *this until one of the following conditions are met:- 文件结束发生在输入序列中;原文:end-of-file occurs on the input sequence;
- 插入输出序列中的失败(在这种情况下,要被插入的字符没有提取);原文:inserting in the output sequence fails (in which case the character to be inserted is not extracted);
- 异常发生时(在这种情况下,异常被捕获).原文:an exception occurs (in which case the exception is caught).
-
如果没有字符被插入,执行setstate(badbit)。如果一个异常被抛出,而提取,设置failbit
9) 原文:
If no characters were inserted, executes setstate(badbit). If an exception was thrown while extracting, sets failbit
调用func(*this);。此重载是用来实现输出I / O操纵,如std::endl.
原文:
Calls func(*this);. This overload is used to implement output I/O manipulators such as std::endl.
目录 |
[编辑] 参数
value | - | 整数,浮点型,布尔型,或指针的值插入
原文: integer, floating-point, boolean, or pointer value to insert |
func | - | 要调用的函数
|
sb | - | 指针的streambuffer阅读
原文: pointer to the streambuffer to read the data from |
[编辑] 返回值
*this
[编辑] 注释
有没有过载,易挥发或函数指针的指针(其他的签名,比那些接受9)过载)。试图输出这些对象调用隐式转换为bool,对于任何非空指针值,该值1打印(除非boolalpha设置),.
原文:
There are no overload for pointers to volatile or function pointers (other than the ones with signatures accepted by the 9) overload). Attempting to output such objects invokes implicit conversion to bool, and, for any non-null pointer value, the value 1 is printed (unless boolalpha was set).
[编辑] 示例
#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 << std::endl; // function overload }
输出:
42 true 3.140000 "Some text." true
[编辑] 另请参阅
插入字符数据 (函数) | |
一个整数值,通过调用执行I / O流的字符串 (函数模板) | |
执行流的输入和输出的位集 原文: performs stream input and output of bitsets (函数) | |
序列化和反序列化一个复杂的数 原文: serializes and deserializes a complex number (函数模板) | |
执行流的输入和输出的伪随机数的发动机 原文: performs stream input and output on pseudo-random number engine (函数) | |
执行流输入和输出的伪随机数分布 原文: performs stream input and output on pseudo-random number distribution (函数) | |
插入一个字符 (公共成员函数) | |
插入的字符块 (公共成员函数) |