std:: istream_iterator
|
헤더 파일에 정의됨
<iterator>
|
||
|
template
<
class
T,
class
CharT
=
char
,
|
(C++17 이전) | |
|
template
<
class
T,
class
CharT
=
char
,
|
(C++17 이후) | |
std::istream_iterator
는 생성된
std::basic_istream
객체로부터
T
타입의 연속적인 객체들을 적절한
operator
>>
를 호출하여 읽는 단일 패스 입력 반복자입니다. 실제 읽기 연산은 반복자가 역참조될 때가 아니라 증가될 때 수행됩니다. 첫 번째 객체는 반복자가 생성될 때 읽힙니다. 역참조는 가장 최근에 읽은 객체의 복사본만 반환합니다.
기본 생성된
std::istream_iterator
는
스트림 끝(end-of-stream)
반복자로 알려져 있습니다. 유효한
std::istream_iterator
가 기본 스트림의 끝에 도달하면, 이는 스트림 끝 반복자와 동일해집니다. 이를 역참조하거나 더 증가시키는 것은 미정의 동작을 유발합니다. 스트림 끝 반복자는 기본 스트림의 상태가 변경되더라도 스트림 끝 상태를 유지합니다. 재할당이 없는 한, 이는 더 이상 비-스트림 끝 반복자가 될 수 없습니다.
std::istream_iterator
의 일반적인 구현은 두 개의 데이터 멤버를 보유합니다: 연결된
std::basic_istream
객체에 대한 포인터와 가장 최근에 읽은
T
타입의 값입니다.
T
는
DefaultConstructible
,
CopyConstructible
, 그리고
CopyAssignable
요구 사항을 충족해야 합니다.
목차 |
멤버 타입
| 멤버 타입 | 정의 |
iterator_category
|
std:: input_iterator_tag |
value_type
|
T |
difference_type
|
Distance |
pointer
|
const T * |
reference
|
const T & |
char_type
|
CharT
|
traits_type
|
Traits
|
istream_type
|
std:: basic_istream < CharT, Traits > |
|
멤버 타입
|
(C++17 이전) |
멤버 함수
새로운
istream_iterator
를 생성합니다
(public member function) |
|
캐시된 값을 포함한
istream_iterator
를 소멸합니다
(public member function) |
|
|
현재 요소를 반환합니다
(public member function) |
|
|
반복자를 전진시킵니다
(public member function) |
비멤버 함수
|
(removed in C++20)
|
두
istream_iterator
를 비교함
(함수 템플릿) |
참고 사항
문자를 읽을 때,
std::istream_iterator
는 기본적으로 공백을 건너뜁니다(
std::noskipws
또는 동등한 방법으로 비활성화하지 않는 한). 반면
std::istreambuf_iterator
는 그렇지 않습니다. 게다가,
std::istreambuf_iterator
는 문자당 센트리 객체를 생성하고 파괴하는 오버헤드를 피하므로 더 효율적입니다.
예제
#include <algorithm> #include <iostream> #include <iterator> #include <numeric> #include <sstream> int main() { std::istringstream str("0.1 0.2 0.3 0.4"); std::partial_sum(std::istream_iterator<double>(str), std::istream_iterator<double>(), std::ostream_iterator<double>(std::cout, " ")); std::istringstream str2("1 3 5 7 8 9 10"); auto it = std::find_if(std::istream_iterator<int>(str2), std::istream_iterator<int>(), [](int i){ return i % 2 == 0; }); if (it != std::istream_iterator<int>()) std::cout << "\nThe first even number is " << *it << ".\n"; //" 9 10" left in the stream }
출력:
0.1 0.3 0.6 1 The first even number is 8.
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| P0738R2 | C++98 | 첫 번째 읽기가 첫 번째 역참조까지 지연될 수 있음 | 항상 생성자에서 수행됨 |
참고 항목
|
std::basic_ostream
에 쓰기를 수행하는 출력 반복자
(클래스 템플릿) |
|
|
std::basic_streambuf
에서 읽기를 수행하는 입력 반복자
(클래스 템플릿) |