std::basic_ostream<CharT,Traits>::operator<<
提供: cppreference.com
<tbody>
</tbody>
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 の場合は、 ios_base::flags() & ios_base::basefield が ios_base::oct または ios_base::hex であれば、それを unsigned short または unsigned int にキャストします。 その後、いずれの場合でも 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 が設定され、 exceptions() に failbit がある場合はその例外が投げ直されます。10-12)
func(*this) を呼びます。 これらのオーバーロードは std::endl のような出力マニピュレータを実装するために使用されます。引数
| value | - | 挿入する整数、浮動小数点、ブーリアンまたはポインタの値 |
| func | - | 呼ぶ関数 |
| sb | - | データを読み込むストリームバッファへのポインタ |
戻り値
1-11)
*this。12)
func(*this)。ノート
非静的メンバへのポインタ、 volatile へのポインタ、または関数ポインタ ((10-12) が取るシグネチャのもの以外) に対するオーバーロードはありません。 そのようなオブジェクトの出力を試みると bool への暗黙の変換が行われ、あらゆる非ヌルなポインタ値に対して値 1 が出力されます (boolalpha が設定されている場合は true が出力されます)。
文字および文字列の引数 (例えば char または const char* 型の) は operator<< の非メンバオーバーロードによって処理されます。 メンバ関数呼び出しの構文を使用して文字の出力を試みる (例えば std::cout.operator<<('c');) と、オーバーロード (2-4) のいずれかが呼ばれ、数値が出力されます。 メンバ関数呼び出しの構文を使用して文字列の出力を試みると、オーバーロード (7) が呼ばれ、代わりにポインタ値が出力されます。
例
Run this code
#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
関連項目
| 文字データを挿入します (関数テンプレート) | |
| 文字列のストリーム入出力を行います (関数テンプレート) | |
| ビットセットのストリーム入出力を行います (関数テンプレート) | |
| 複素数をシリアライズおよびデシリアライズします (関数テンプレート) | |
| 擬似乱数エンジンに対してストリーム入出力を行います (関数テンプレート) | |
| 乱数分布に対してストリーム入出力を行います (関数テンプレート) | |
| 文字を挿入します (パブリックメンバ関数) | |
| 文字のブロックを挿入します (パブリックメンバ関数) | |
(C++17) |
整数値または浮動小数点値を文字シーケンスに変換します (関数) |