次の方法で共有


ostream_iterator クラス

クラス テンプレート ostream_iterator は、抽出 を使用して連続する要素を出力ストリームに書き込む出力反復子オブジェクトを表します。

構文

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

パラメーター

Type
出力ストリームに挿入されるオブジェクトの型。

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

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

ostream_iterator クラスは出力反復子の要件を満たす必要があります。 アルゴリズムは を使用して出力ストリームに直接書き込むことができます。

コンストラクター

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

Typedefs

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

演算子

演算子 説明
operator* 出力反復子式 を実装するために使用される逆参照演算子。
operator++ 操作が呼び出される前に示したものと同じオブジェクトに を返す、実質的な機能を持たないインクリメント演算子。
operator= 出力ストリームに書き込むための出力反復子式 を実装するために使用される代入演算子。

要件

ヘッダー:

名前空間:

ostream_iterator::char_type

反復子の文字型を提供する型。

typedef CharType char_type;

解説

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

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

int main( )
{
   using namespace std;

   typedef ostream_iterator<int>::char_type CHT1;
   typedef ostream_iterator<int>::traits_type CHTR1;

   // ostream_iterator for stream cout
   // with new line delimiter:
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream:
   cout << "The integers written to the output stream\n"
        << "by intOut are:" << endl;
*intOut = 10;
*intOut = 20;
*intOut = 30;
}
The integers written to the output stream
by intOut are:
10
20
30

ostream_iterator::operator*

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

ostream_iterator<Type, CharType, Traits>& operator*();

戻り値

への参照。

解説

が満たす必要がある出力反復子の要件では、式有効である必要があり、やについては何も記述しません。 この実装のメンバー演算子は、 を返します。

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *intOut = 10;
   intOut++;      // No effect on iterator position
   *intOut = 20;
   *intOut = 30;
}
Elements written to output stream:
10
20
30

ostream_iterator::operator++

操作が呼び出される前に示したものと同じオブジェクトに を返す、実質的な機能を持たないインクリメント演算子。

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

戻り値

への参照。

解説

これらのメンバー演算子はどちらも を返します。

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *intOut = 10;
   intOut++;      // No effect on iterator position
   *intOut = 20;
   *intOut = 30;
}
Elements written to output stream:
10
20
30

ostream_iterator::operator=

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

ostream_iterator<Type, CharType, Traits>& operator=(const Type& val);

パラメーター

val
出力ストリームに挿入される 型のオブジェクトの値。

戻り値

演算子は、オブジェクトに関連付けられている出力ストリームに を挿入し、その後に で指定された区切り記号 (存在する場合) を挿入し、 への参照を返します。

解説

が満たす必要がある出力反復子の要件では、式有効である必要があり、演算子または演算子について何も言いません。 このメンバー演算子は、 を返します。

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
   *intOut = 10;
   intOut++;      // No effect on iterator position
   *intOut = 20;
   *intOut = 30;
}
Elements written to output stream:
10
20
30

ostream_iterator::ostream_iterator

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

ostream_iterator(ostream_type& _Ostr);

ostream_iterator(
    ostream_type& _Ostr,
    const CharType* _Delimiter);

パラメーター

_Ostr
反復処理する 型の出力ストリーム。

_Delimiter
出力ストリームで値の間に挿入される区切り記号。

解説

最初のコンストラクターは、出力ストリーム ポインターを で初期化します。 区切り記号文字列ポインターは、空の文字列を指定します。

2 番目のコンストラクターは、 を使用して出力ストリーム ポインターを初期化し、区切り記号文字列ポインターを で初期化します。

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   ostream_iterator<int> intOut ( cout , "\n" );
   *intOut = 10;
   intOut++;
   *intOut = 20;
   intOut++;

   int i;
   vector<int> vec;
   for ( i = 1 ; i < 7 ; ++i )
   {
      vec.push_back (  i );
   }

   // Write elements to standard output stream
   cout << "Elements output without delimiter: ";
   copy ( vec.begin ( ), vec.end ( ),
          ostream_iterator<int> ( cout ) );
   cout << endl;

   // Write elements with delimiter " : " to output stream
   cout << "Elements output with delimiter: ";
   copy ( vec.begin ( ), vec.end ( ),
          ostream_iterator<int> ( cout, " : " ) );
   cout << endl;
}
10
20
Elements output without delimiter: 123456
Elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 :

ostream_iterator::ostream_type

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

typedef basic_ostream<CharType, Traits> ostream_type;

解説

この型は、 のシノニムであり、書き込みに使用できるオブジェクトを定義する iostream 階層のストリーム クラスです。

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

ostream_iterator::traits_type

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

typedef Traits traits_type;

解説

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

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

int main( )
{
   using namespace std;

   // The following not OK, but are just the default values:
   typedef ostream_iterator<int>::char_type CHT1;
   typedef ostream_iterator<int>::traits_type CHTR1;

   // ostream_iterator for stream cout
   // with new line delimiter:
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream:
   cout << "The integers written to output stream\n"
        << "by intOut are:" << endl;
*intOut = 1;
*intOut = 10;
*intOut = 100;
}
The integers written to output stream
by intOut are:
1
10
100

関連項目

<iterator>
C++ 標準ライブラリ内のスレッド セーフ
C++ 標準ライブラリ リファレンス