istream_iterator クラス

入力反復子オブジェクトを表します。 このクラスは、入力ストリームから Type クラスのオブジェクトを抽出します。これには、格納している basic_istream<CharType, Traits> への pointer 型のオブジェクトを介してアクセスします。

構文

template <class Type, class CharType = char, class Traits = char_traits<CharType>, class Distance = ptrdiff_t>
class istream_iterator
: public iterator<
    input_iterator_tag, Type, Distance,
    const Type *,
    const Type&>;

パラメーター

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

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

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

距離
istream_iterator の相違点の種類を表す符号付き整数型。 この引数は省略可能であり、既定値は ptrdiff_t です。

null 以外の格納されたポインターを使用して istream_iterator クラスのオブジェクトを構築またはインクリメントすると、オブジェクトは、関連付けられている入力ストリームから Type 型のオブジェクトを抽出および格納することを試行します。 抽出が失敗した場合、オブジェクトは効果的に格納されたポインターを null ポインターに置き換え、シーケンス終端のインジケーターを作成します。

コンストラクター

コンストラクター 説明
istream_iterator 既定の istream_iterator または読み取り元の反復子のストリーム型に初期化される istream_iterator として、ストリームの終わり反復子を構築します。

Typedefs

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

演算子

演算子 説明
operator* 逆参照演算子は、Type で指定された istream_iterator 型の格納されたオブジェクトを返します。
operator-> メンバーの値 (存在する場合) を返します。
operator++ 入力ストリームからインクリメントされたオブジェクトを抽出するか、オブジェクトをインクリメントする前にオブジェクトをコピーして、そのコピーを返します。

要件

ヘッダー: <iterator>

名前空間: std

istream_iterator::char_type

istream_iterator の文字型を提供する型。

typedef CharType char_type;

解説

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

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

int main( )
{
   using namespace std;

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

   // Standard iterator interface for reading
   // elements from the input stream:
   cout << "Enter integers separated by spaces & then\n"
        << " any character ( try example: '2 4 f' ): ";

   // istream_iterator for reading int stream
   istream_iterator<int, CHT1, CHTR1> intRead ( cin );

   // End-of-stream iterator
   istream_iterator<int, CHT1, CHTR1> EOFintRead;

   while ( intRead != EOFintRead )
   {
      cout << "Reading:  " << *intRead << endl;
      ++intRead;
   }
   cout << endl;
}

istream_iterator::istream_iterator

既定の istream_iterator または読み取り元の反復子のストリーム型に初期化される istream_iterator として、ストリームの終わり反復子を構築します。

istream_iterator();

istream_iterator(istream_type& _Istr);

パラメーター

_Istr
istream_iterator を初期化するために読み込まれる入力ストリーム。

解説

最初のコンストラクターは、null ポインターを使用して入力ストリーム ポインターを初期化し、ストリームの終わり反復子を作成します。 2 番目のコンストラクターは、 &_Istr を使用して入力ストリーム ポインターを初期化し、 Type型のオブジェクトを抽出して格納しようとします。

ストリームの終了反復子を使用して、 istream_iterator がストリームの末尾に達したかどうかをテストできます。

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

int main( )
{
   using namespace std;

   // Used in conjunction with copy algorithm
   // to put elements into a vector read from cin
   vector<int> vec ( 4 );
   vector <int>::iterator Iter;

   cout << "Enter 4 integers separated by spaces & then\n"
        << " a character ( try example: '2 4 6 8 a' ): ";
   istream_iterator<int> intvecRead ( cin );

   // Default constructor will test equal to end of stream
   // for delimiting source range of vecor
   copy ( intvecRead , istream_iterator<int>( ) , vec.begin ( ) );
   cin.clear ( );

   cout << "vec = ";
   for ( Iter = vec.begin( ) ; Iter != vec.end( ) ; Iter++ )
      cout << *Iter << " "; cout << endl;
}

istream_iterator::istream_type

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

typedef basic_istream<CharType, Traits> istream_type;

解説

この型は、basic_istream<CharType, Traits> の同意語です。

istream_type を宣言して使用する方法の例については、istream_iterator に関するセクションを参照してください。

istream_iterator::operator*

逆参照演算子は、Type で指定された istream_iterator 型の格納されたオブジェクトを返します。

const Type& operator*() const;

戻り値

Type 型の格納されているオブジェクト。

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

int main( )
{
   using namespace std;

   cout << "Enter integers separated by spaces & then\n"
        << " a character ( try example: '2 4 6 8 a' ): ";

   // istream_iterator from stream cin
   istream_iterator<int> intRead ( cin );

   // End-of-stream iterator
   istream_iterator<int> EOFintRead;

   while ( intRead != EOFintRead )
   {
      cout << "Reading:  " << *intRead << endl;
      ++intRead;
   }
   cout << endl;
}

istream_iterator::operator->

メンバーの値 (存在する場合) を返します。

const Type* operator->() const;

戻り値

メンバーの値 (存在する場合)。

解説

i->m(*i).m と同じです。

この演算子は &*this を返します。

// istream_iterator_operator_vm.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>
#include <complex>

using namespace std;

int main( )
{
   cout << "Enter complex numbers separated by spaces & then\n"
        << " a character pair ( try example: '(1,2) (3,4) (a,b)' ): ";

   // istream_iterator from stream cin
   istream_iterator< complex<double> > intRead ( cin );

   // End-of-stream iterator
   istream_iterator<complex<double> > EOFintRead;

   while ( intRead != EOFintRead )
   {
      cout << "Reading the real part: " << intRead ->real( ) << endl;
      cout << "Reading the imaginary part: " << intRead ->imag( ) << endl;
      ++intRead;
   }
   cout << endl;
}

istream_iterator::operator++

入力ストリームからインクリメントされたオブジェクトを抽出するか、オブジェクトをインクリメントする前にオブジェクトをコピーして、そのコピーを返します。

istream_iterator<Type, CharType, Traits, Distance>& operator++();

istream_iterator<Type, CharType, Traits, Distance> operator++(int);

戻り値

最初のメンバー演算子は、入力ストリームから抽出された Type 型のインクリメントされたオブジェクトへの参照を返し、2 番目のメンバー関数が、このオブジェクトのコピーを返します。

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

int main( )
{
   using namespace std;

   cout << "Enter integers separated by spaces & then\n"
        << " a character ( try example: '2 4 6 8 a' ): ";

   // istream_iterator from stream cin
   istream_iterator<int> intRead ( cin );

   // End-of-stream iterator
   istream_iterator<int> EOFintRead;

   while ( intRead != EOFintRead )
   {
      cout << "Reading:  " << *intRead << endl;
      ++intRead;
   }
   cout << endl;
}

istream_iterator::traits_type

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

typedef Traits traits_type;

解説

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

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

int main( )
{
   using namespace std;

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

   // Standard iterator interface for reading
   // elements from the input stream:
   cout << "Enter integers separated by spaces & then\n"
        << " any character ( try example: '10 20 a' ): ";

   // istream_iterator for reading int stream
   istream_iterator<int, CHT1, CHTR1> intRead ( cin );

   // End-of-stream iterator
   istream_iterator<int, CHT1, CHTR1> EOFintRead;

   while ( intRead != EOFintRead )
   {
      cout << "Reading:  " << *intRead << endl;
      ++intRead;
   }
   cout << endl;
}

関連項目

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