Namespaces
Variants

std::istreambuf_iterator

cppreference.net에서
 
 
반복자 라이브러리
반복자 개념
반복자 기본 요소
알고리즘 개념 및 유틸리티
간접 호출 가능 개념
일반 알고리즘 요구 사항
(C++20)
(C++20)
(C++20)
유틸리티
(C++20)
반복자 어댑터
범위 접근
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
헤더 <iterator>
template< class CharT, class Traits = std::char_traits<CharT> >
class istreambuf_iterator
    : public std::iterator<std::input_iterator_tag,
                           CharT, typename Traits::off_type,
                           /* unspecified */, CharT>
(C++17까지)
template< class CharT, class Traits = std::char_traits<CharT> >
class istreambuf_iterator;
(C++17부터)

std::istreambuf_iterator는 자신이 생성된 std::basic_streambuf 객체로부터 연속적인 문자를 읽는 단일 패스 입력 반복자입니다.

기본 생성된 std::istreambuf_iterator는 스트림 끝 반복자로 알려져 있습니다. std::istreambuf_iterator가 기본 스트림의 끝에 도달하면 스트림 끝 반복자와 같아집니다. 이를 역참조하거나 증가시키면 정의되지 않은 동작이 발생합니다.

std::istreambuf_iterator는 사소한 복사 생성자, constexpr 기본 생성자, 사소한 소멸자를 가집니다.

(C++11부터)

멤버 타입

멤버 타입 정의
iterator_category std:: input_iterator_tag
value_type CharT
difference_type typename Traits :: off_type
pointer /* unspecified */
reference CharT
char_type CharT
traits_type Traits
int_type typename Traits :: int_type
streambuf_type std:: basic_streambuf < CharT, Traits >
istream_type std:: basic_istream < CharT, Traits >
/* proxy */ 구현 정의 클래스 타입.
proxy 객체는 char_type 문자와 streambuf_type* 포인터를 보유합니다.
proxy 객체를 operator* 로 역참조하면 저장된 문자가 반환됩니다.
( 설명 전용 멤버 타입* )

멤버 타입 iterator_category , value_type , difference_type , pointer 그리고 reference 는 std:: iterator < std:: input_iterator_tag , CharT, typename Traits :: off_type , /* unspecified */ , CharT > 로부터 상속받아 얻어야 합니다.

(C++17 이전)

멤버 타입 pointer 는 일반적으로 CharT* 입니다( 아래 참조).

멤버 함수

새로운 istreambuf_iterator 를 생성합니다
(public member function)
(destructor)
(implicitly declared)
istreambuf_iterator 를 소멸합니다
(public member function)
현재 문자를 복사하여 반환합니다
(public member function)
반복자를 전진시킵니다
(public member function)
두 istreambuf_iterator 가 모두 스트림 끝이거나 모두 유효한지 테스트합니다
(public member function)

비멤버 함수

(C++20에서 제거됨)
두 istreambuf_iterator 를 비교함
(함수 템플릿)

참고 사항

LWG 이슈 659 의 해결은 operator - > 를 도입했습니다. std::istreambuf_iterator i 가 주어졌을 때, 표현식 ( * i ) . m 와 i - > m 이 동일한 효과를 가질 것으로 예상됩니다.

그러나 이 결의안은 해당 동작에 대한 공식적인 명세를 제공하지 않습니다. 따라서 구현마다 다르게 동작하며, nullptr 를 반환하거나, 임시 객체의 주소를 반환하거나, 아예 해당 멤버를 제공하지 않는 경우도 있습니다. 의도된 동작을 달성하기 어려워 LWG issue 2790 의 결의안을 통해 제거되었습니다.

LWG 이슈 659 의 해결은 또한 operator-> 가 프록시를 반환할 수 있도록 하기 위해 멤버 타입 pointer 를 미지정(unspecified)으로 만들었습니다. 이는 CharT 가 클래스 타입이 아닐 때 operator-> 가 컴파일되도록 하기 위함입니다.

예제

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
int main()
{
    // 일반적인 사용 사례: 반복자 쌍으로 표현된 입력 스트림
    std::istringstream in{"Hello, world"};
    std::istreambuf_iterator<char> it{in}, end;
    std::string ss{it, end};
    std::cout << "ss has " << ss.size() << " bytes; "
                 "it holds \"" << ss << "\"\n";
    // 단일 패스 특성의 데모
    std::istringstream s{"abc"};
    std::istreambuf_iterator<char> i1{s}, i2{s};
    std::cout << "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
    ++i1;
    std::cout << "after incrementing i1, but not i2:\n"
                 "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
    ++i2;
    std::cout << "after incrementing i2, but not i1:\n"
                 "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
}

출력:

ss has 12 bytes; it holds "Hello, world"
i1 returns 'a'
i2 returns 'a'
after incrementing i1, but not i2:
i1 returns 'b'
i2 returns 'b'
after incrementing i2, but not i1:
i1 returns 'c'
i2 returns 'c'

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 659 C++98 1. std::istreambuf_iterator 에 operator - > 가 없었음
2. 멤버 타입 pointer 가 CharT* 로 지정됨
1. 추가됨
2. 지정되지 않음으로 변경됨
LWG 2790 C++98 LWG 이슈 659 에 의해 추가된 operator - > 가 유용하지 않았음 제거됨

참고 항목

std::basic_streambuf 에 쓰기를 수행하는 출력 반복자
(클래스 템플릿)
std::basic_istream 에서 읽기를 수행하는 입력 반복자
(클래스 템플릿)