次の方法で共有


クラス

クラス テンプレート は、抽出 を使用して出力ストリームに連続する文字要素を書き込む出力反復子オブジェクトを記述します。 は、出力ストリームに挿入されるオブジェクトの型にジェネリック型ではなく文字を持つ クラスとは異なります。

構文

template <class CharType = char, class Traits = char_traits <CharType>>

パラメーター

CharType
ostreambuf_iterator の文字型を表す型。 この引数は省略可能であり、既定値は です。

Traits
ostreambuf_iterator の文字型を表す型。 この引数は省略可能であり、既定値は です。

解説

ostreambuf_iterator クラスは出力反復子の要件を満たす必要があります。 アルゴリズムは を使用して出力ストリームに直接書き込むことができます。 このクラスは、生の (フォーマットされていない) I/O ストリームに文字の形式でアクセスできる低レベルのストリームの反復子を提供し、高レベルのストリーム反復子に関連付けられたバッファリングや文字変換をバイパスすることができます。

コンストラクター

コンストラクター 説明
ostreambuf_iterator 出力ストリームに文字を書き込むために初期化された を構築します。

Typedefs

型名 説明
char_type の文字型を提供する型。
ostream_type のストリーム型を提供する型。
streambuf_type のストリーム型を提供する型。
traits_type の文字特性型を提供する型。

メンバー関数

メンバー関数 説明
failed 出力ストリーム バッファーへの挿入の失敗をテストします。

演算子

演算子 説明
operator* 出力反復子式 を実装するために使用される逆参照演算子。
operator++ 操作が呼び出される前に示したものと同じオブジェクトに を返す、実質的な機能を持たないインクリメント演算子。
operator= この演算子は、関連付けられているストリーム バッファーに文字を挿入します。

要件

ヘッダー:

名前空間:

ostreambuf_iterator::char_type

の文字型を提供する型。

typedef CharType char_type;

解説

この型は、テンプレート パラメーター のシノニムです。

// ostreambuf_iterator_char_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;

   typedef ostreambuf_iterator<char>::char_type CHT1;
   typedef ostreambuf_iterator<char>::traits_type CHTR1;

   // ostreambuf_iterator for stream cout
   // with new line delimiter:
    ostreambuf_iterator< CHT1, CHTR1> charOutBuf ( cout );

   // Standard iterator interface for writing
   // elements to the output streambuf:
   cout << "The characters written to the output stream\n"
        << " by charOutBuf are: ";
   *charOutBuf = 'O';
   charOutBuf++;
   *charOutBuf = 'U';
   charOutBuf++;
   *charOutBuf = 'T';
   charOutBuf++;
   cout << "." << endl;
}
OUT.

ostreambuf_iterator::failed

出力ストリーム バッファーへの挿入の失敗をテストします。

bool failed() const throw();

戻り値

これまでに出力ストリーム バッファーへの挿入が失敗していない場合は、。そうでない場合は、。

解説

メンバー関数は、出力ストリーム バッファーに文字を挿入する前の試行が失敗した場合に、 を返します。

// ostreambuf_iterator_failed.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;

   // ostreambuf_iterator for stream cout
   ostreambuf_iterator<char> charOut ( cout );

   *charOut = 'a';
   charOut ++;
   *charOut  = 'b';
   charOut ++;
   *charOut = 'c';
   cout << " are characters output individually." << endl;

   bool b1 = charOut.failed ( );
   if (b1)
       cout << "At least one insertion failed." << endl;
   else
       cout << "No insertions failed." << endl;
}
abc are characters output individually.
No insertions failed.

ostreambuf_iterator::operator*

出力反復子式 を実装するために使用される非機能逆参照演算子。

ostreambuf_iterator<CharType, Traits>& operator*();

戻り値

ostreambuf 反復子オブジェクト。

解説

この演算子は、出力反復子式でのみ機能し、ストリーム バッファーに文字を出力 。 に適用され、反復子を返します。はを返します。

// ostreambuf_iterator_op_deref.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main()
{
   using namespace std;

   // ostreambuf_iterator for stream cout
   // with new line delimiter
   ostreambuf_iterator<char> charOutBuf ( cout );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *charOutBuf = 'O';
   charOutBuf++;   // no effect on iterator position
   *charOutBuf = 'U';
   *charOutBuf = 'T';
}
Elements written to output stream:
OUT

ostreambuf_iterator::operator++

操作が呼び出される前に指定したのと同じ文字に を返す非機能インクリメント演算子。

ostreambuf_iterator<CharType, Traits>& operator++();
ostreambuf_iterator<CharType, Traits>& operator++(int);

戻り値

最初にアドレス指定された文字、または に変換できる実装定義オブジェクトへの参照。

解説

この演算子は、出力反復子式 を実装するために使用されます。

// ostreambuf_iterator_op_incr.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;

   // ostreambuf_iterator for stream cout
   // with new line delimiter
   ostreambuf_iterator<char> charOutBuf ( cout );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *charOutBuf = 'O';
   charOutBuf++;      // No effect on iterator position
   *charOutBuf = 'U';
   *charOutBuf = 'T';
}
Elements written to output stream:
OUT

ostreambuf_iterator::operator=

この演算子は、関連付けられているストリーム バッファーに文字を挿入します。

ostreambuf_iterator<CharType, Traits>& operator=(CharType _Char);

パラメーター

_Char
ストリーム バッファーに挿入する文字。

戻り値

ストリーム バッファーに挿入された文字への参照。

解説

出力ストリームに書き込むための出力反復子式 を実装するために使用される代入演算子。

// ostreambuf_iterator_op_assign.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;

   // ostreambuf_iterator for stream cout
   // with new line delimiter
   ostreambuf_iterator<char> charOutBuf ( cout );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *charOutBuf = 'O';
   charOutBuf++;      // No effect on iterator position
   *charOutBuf = 'U';
   *charOutBuf = 'T';
}
Elements written to output stream:
OUT

ostreambuf_iterator::ostreambuf_iterator

出力ストリームに文字を書き込むために初期化された を構築します。

ostreambuf_iterator(streambuf_type* strbuf) throw();
ostreambuf_iterator(ostream_type& Ostr) throw();

パラメーター

strbuf
出力ストリームバッファー ポインターを初期化するために使用される出力 streambuf オブジェクト。

Ostr
出力ストリームバッファー ポインターを初期化するために使用される出力ストリーム オブジェクト。

解説

最初のコンストラクターは、 を使用して出力ストリーム バッファー ポインターを初期化します。

2 番目のコンストラクターは、* を使用して出力ストリーム バッファー ポインターを初期化します。 格納されたポインターは null ポインターでない必要があります。

// ostreambuf_iteratorOstreambuf_iterator.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main()
{
   using namespace std;

   // ostreambuf_iterator for stream cout
   ostreambuf_iterator<char> charOut ( cout );

   *charOut = 'O';
   charOut ++;
   *charOut  = 'U';
   charOut ++;
   *charOut = 'T';
   cout << " are characters output individually." << endl;

   ostreambuf_iterator<char> strOut ( cout );
   string str = "These characters are being written to the output stream.\n ";
   copy ( str.begin ( ), str. end ( ), strOut );
}
OUT are characters output individually.
These characters are being written to the output stream.

ostreambuf_iterator::ostream_type

のストリーム型を提供する型。

typedef basicOstream<CharType, Traits> ostream_type;

解説

型は次のシノニムです。

を宣言して使用する方法の例については、を参照してください。

ostreambuf_iterator::streambuf_type

のストリーム型を提供する型。

typedef basic_streambuf<CharType, Traits> streambuf_type;

解説

この型は、のシノニムであり、文字型に特化するとになる I/O バッファーのストリーム クラスです。

を宣言して使用する方法の例については、を参照してください。

ostreambuf_iterator::traits_type

の文字特性型を提供する型。

typedef Traits traits_type;

解説

この型は、テンプレート パラメーター のシノニムです。

// ostreambuf_iterator_traits_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;

   typedef ostreambuf_iterator<char>::char_type CHT1;
   typedef ostreambuf_iterator<char>::traits_type CHTR1;

   // ostreambuf_iterator for stream cout
   // with new line delimiter:
    ostreambuf_iterator< CHT1, CHTR1> charOutBuf ( cout );

   // Standard iterator interface for writing
   // elements to the output streambuf:
   cout << "The characters written to the output stream\n"
        << " by charOutBuf are: ";
   *charOutBuf = 'O';
   charOutBuf++;
   *charOutBuf = 'U';
   charOutBuf++;
   *charOutBuf = 'T';
   charOutBuf++;
   cout << "." << endl;
}
The characters written to the output stream
by charOutBuf are: OUT.

関連項目

反復 子
C++ 標準ライブラリ内のスレッド セーフ
C++ 標準ライブラリ リファレンス