名前空間
変種
操作

operator<<(std::basic_ostream)

提供: cppreference.com
< cpp‎ | io‎ | basic ostream
 
 
 
 
(1)
template<class CharT, class Traits>

basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,

                                         CharT ch );
template<class CharT, class Traits>

basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,

                                         char ch );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,

                                        char ch );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,

                                        signedchar ch );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,

                                        unsignedchar ch );
(2)
template<class CharT, class Traits >

basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,

                                         const CharT* s );
template<class CharT, class Traits >

basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,

                                         constchar* s );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,  

                                        constchar* s );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,

                                        constsignedchar* s );
template<class Traits >

basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,

                                        constunsignedchar* s );
template<class CharT, class Traits, class T >

basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os,

                                            const T& value );
(3) (C++11以上)

文字または文字列を挿入します。

1)FormattedOutputFunction として動作します。 sentry オブジェクトの構築および確認の後、文字 ch を挿入します。 文字の型が CharT でない場合は、まず os.widen(ch) で変換されます。 パディングは以下のように決定されます。 os.width()>1 であれば、出力文字シーケンスを形成するために出力文字に os.width()-1 個の os.fill() のコピーが追加されます。 (out.flags()&std::ios_base::adjustfield)==std::ios_base::left の場合はフィル文字は出力文字の後に、そうでなければ前に配置されます。 挿入の後、 std::setw の効果 (もしあれば) を取り消すために os.width(0) が呼ばれます。
2)FormattedOutputFunction として動作します。 sentry オブジェクトの構築および確認の後、最初の要素が s によって指されている文字配列から連続する文字が挿入されます。
  • 1つめと3つめのオーバーロード (CharTch の型と一致する) の場合は、ちょうど traits::length(s) 個の文字が挿入されます。
  • 2つめのオーバーロードの場合は、ちょうど std::char_traits<char>::length(s) 個の文字が挿入されます。
  • 最後の2つのオーバーロードの場合は、ちょうど traits::length(reinterpret_cast<constchar*>(s)) 個の文字が挿入されます。

挿入の前に、まず os.widen() を用いてすべての文字がワイド化され、その後パディングが以下のように決定されます。 挿入する文字数が os.width() より少ない場合は、長さを os.width() と等しくするために文字シーケンスに十分な数の os.fill() のコピーが追加されます。 (out.flags()&std::ios_base::adjustfield)==std::ios_base::left の場合はフィル文字は出力シーケンスの終端に追加され、そうでなければフィル文字は出力シーケンスの前に追加されます。 挿入の後、 std::setw の効果 (もしあれば) を取り消すために os.width(0) が呼ばれます。

s がヌルポインタの場合、動作は未定義です。
3) 出力ストリームオブジェクトへの右辺値参照を指定して、適切な挿入演算子を呼びます (os << value と同等です)。 この関数テンプレートは、式 os << value が well-formed でなければ、オーバーロード解決に参加しません。(C++17以上)

目次

[編集]引数

os - データを挿入する出力ストリーム
ch - 挿入する文字への参照
s - 挿入する文字列へのポインタ

[編集]戻り値

os

[編集]ノート

LLVM libc++ のオーバーロード (3)LWG#1203 を実装しており、 (std::ostringstream()<<1.2).str() のようなコードがコンパイルできるように、引数と同じ型のストリームを返します。

[編集]

#include <iostream>#include <fstream>   int main(){std::cout<<"Hello, world"// the const char* overload<<'\n';// the char overloadstd::ofstream("test.txt")<<1.2;// rvalue overload}

出力:

Hello, world

[編集]関連項目

書式付きデータを挿入します
(パブリックメンバ関数)[edit]
文字をワイド化します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数)[edit]
close