다음을 통해 공유


istream_iterator 클래스

입력 반복기 개체에 대해 설명합니다. 저장한 개체를 통해 액세스하는 입력 스트림에서 클래스 TypeCharType>pointerbasic_istream<Traits개체를 추출합니다.

구문

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입니다.

특성
istream_iterator의 문자 형식을 나타내는 형식입니다. 이 인수는 선택 사항이며 기본값은 char_traits<CharType>입니다.

거리
istream_iterator에 대해 차이 형식을 나타내는 부호 있는 정수 계열 형식입니다. 이 인수는 선택 사항이며 기본값은 ptrdiff_t입니다.

null이 아닌 저장된 포인터를 사용하여 istream_iterator 클래스를 구성 또는 증가한 이후 개체는 연결된 입력 스트림에서 Type 형식의 개체에 대해 효과적인 추출 및 저장을 시도합니다. 추출이 실패할 경우 개체는 저장된 포인터를 null 포인터로 대체하여 시퀀스 끝 표시기를 만듭니다.

생성자

생성자 Description
istream_iterator 기본 istream_iterator로 스트림의 끝 반복기를 구성하거나 반복기의 스트림 형식에 초기화된 istream_iterator로 구성합니다.

Typedef

형식 이름 설명
char_type istream_iterator의 문자 형식을 허용하는 형식입니다.
istream_type istream_iterator의 스트림 형식을 허용하는 형식입니다.
traits_type istream_iterator의 특성 형식을 허용하는 형식입니다.

연산자

연산자 설명
operator* 역참조 연산자는 Type에서 주소 지정하는 형식 istream_iterator의 저장된 개체를 반환합니다.
operator-> 멤버의 값을 반환합니다(있는 경우).
operator++ 입력 스트림에서 증가된 개체를 추출하거나 개체를 증가하기 전에 복사하여 복사본을 반환합니다.

요구 사항

헤더:<반복기>

네임스페이스: 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 포인터로 초기화하고 스트림의 끝 반복기를 만듭니다. 두 번째 생성자는 &_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;

설명

이 형식은 CharType, Traits의 동의어basic_istream<입니다>.

예시

istream_type을 선언하고 사용하는 방법에 대한 예제는 istream_iterator를 참조하세요.

istream_iterator::operator*

역참조 연산자는 Type에서 주소 지정하는 형식 istream_iterator의 저장된 개체를 반환합니다.

const Type& operator*() const;

Return Value

형식 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;

Return Value

멤버의 값입니다(있는 경우).

설명

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);

Return Value

첫 번째 멤버 연산자는 입력 스트림에서 추출된 형식 Type 의 증분 개체에 대한 참조를 반환하고 두 번째 멤버 함수는 개체의 복사본을 반환합니다.

예시

// 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++ 표준 라이브러리 참조