std::istreambuf_iterator

std::istreambuf_iterator (입력 버퍼 반복자)

생성된 std::basic_streambuf 객체에서 연속된 문자를 읽는 단일 패스 입력 반복자예요. C++11부터 있어요.

출처: cppreference

본문

<iterator> 헤더에 정의돼 있어요.

// C++17 이전
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;

std::istreambuf_iterator는 생성된 std::basic_streambuf 객체에서 연속된 문자를 읽는 단일 패스 입력 반복자예요. 기본 생성된 std::istreambuf_iterator는 end-of-stream 반복자로 알려져 있어요. std::istreambuf_iterator가 기반 스트림의 끝에 도달하면 end-of-stream 반복자와 같아져요. 그것을 역참조하거나 더 증가시키면 정의되지 않은 동작이에요. std::istreambuf_iterator는 trivial 복사 생성자, constexpr 기본 생성자 등을 가져요.

멤버 타입

멤버 타입 정의
iterator_category std::input_iterator_tag
value_type CharT
difference_type Traits::off_type
pointer (미지정)
reference CharT
char_type CharT
traits_type Traits
int_type Traits::int_type
streambuf_type std::basic_streambuf<CharT, Traits>
istream_type std::basic_istream<CharT, Traits>

멤버 함수

  • (constructor) — 입력 버퍼 반복자를 생성해요(기본이면 끝 반복자).
  • operator= — 내용을 할당해요.
  • operator* — 현재 문자를 돌려줘요.
  • operator++ — 다음 문자로 나아가요.
  • equal — end-of-stream 상태를 검사해요.

비멤버 함수

  • operator== — 두 반복자를 비교(끝 반복자 판정 포함).

예제

#include <iterator>
#include <sstream>
#include <iostream>

int main()
{
    std::istringstream str("hello");
    std::istreambuf_iterator<char> it(str), end;
    std::string s(it, end);
    std::cout << s << '\n'; // "hello"
}

더 알아보기 (Learn more)

cppreference