std::ostreambuf_iterator

std::ostreambuf_iterator (출력 버퍼 반복자)

생성된 std::basic_streambuf 객체에 연속된 문자를 쓰는 단일 패스 LegacyOutputIterator예요. C++11부터 있어요.

출처: cppreference

본문

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

// C++17 이전
template< class CharT, class Traits = std::char_traits<CharT> >
class ostreambuf_iterator
    : public std::iterator<std::output_iterator_tag, void, void, void, void>;

// C++17부터
template< class CharT, class Traits = std::char_traits<CharT> >
class ostreambuf_iterator;

std::ostreambuf_iterator는 생성된 std::basic_streambuf 객체에 연속된 문자를 쓰는 단일 패스 LegacyOutputIterator예요. 실제 쓰기 연산은 반복자(역참조 여부와 무관)에 대입할 때 수행돼요. std::ostreambuf_iterator를 증가시키는 것은 no-op이에요. 전형적인 구현에서 std::ostreambuf_iterator의 유일한 데이터 멤버는 연관된 std::basic_streambuf에 대한 포인터와 end-of-file 조건에 도달했는지 나타내는 불리언 플래그예요.

멤버 타입

멤버 타입 정의
iterator_category std::output_iterator_tag
value_type void
difference_type void
pointer void
reference void
char_type CharT
traits_type Traits
streambuf_type std::basic_streambuf<CharT, Traits>
ostream_type std::basic_ostream<CharT, Traits>

멤버 함수

  • (constructor) — 출력 버퍼 반복자를 구성해요.
  • operator= — 버퍼에 문자를 써요(sbuf->sputc(value)).
  • operator*, operator++, operator++(int) — no-op.
  • failed — 이전 쓰기 연산이 실패했는지 검사해요.

예제

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

int main()
{
    std::ostringstream str;
    std::ostreambuf_iterator<char> it(str);
    *it = 'h'; ++it;
    *it = 'i';
    std::cout << str.str() << '\n'; // "hi"
}

더 알아보기 (Learn more)

cppreference