std::istream_iterator
| 헤더에 정의됨 <iterator>
|
||
template< class T,
class CharT = char,
class Traits = std::char_traits<CharT>,
class Distance = std::ptrdiff_t >
class istream_iterator
: public std::iterator<std::input_iterator_tag, T, Distance, const T*, const T&>
|
(C++17까지) | |
template< class T,
class CharT = char,
class Traits = std::char_traits<CharT>,
class Distance = std::ptrdiff_t >
class istream_iterator;
|
(C++17부터) | |
std::istream_iterator는 T 유형의 연속적인 객체를 std::basic_istream 객체에서 적절한 operator>>를 호출하여 읽는 단일 패스 입력 반복자입니다. 실제 읽기 연산은 반복자가 역참조될 때가 아니라 증가될 때 수행됩니다. 첫 번째 객체는 반복자가 생성될 때 읽힙니다. 역참조는 가장 최근에 읽은 객체의 복사본만 반환합니다.
기본 생성된 std::istream_iterator는 end-of-stream 반복자로 알려져 있습니다. 유효한 std::istream_iterator가 기본 스트림의 끝에 도달하면 end-of-stream 반복자와 같아집니다. 이를 더 이상 역참조하거나 증가시키면 정의되지 않은 동작이 발생합니다. end-of-stream 반복자는 기본 스트림의 상태가 변경되더라도 end-of-stream 상태를 유지합니다. 재할당이 없으면 더 이상 end-of-stream이 아닌 반복자가 될 수 없습니다.
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
에서 읽기를 수행하는 입력 반복자
(클래스 템플릿) |