std::ostream_iterator

std::ostream_iterator (출력 스트림 반복자)

생성된 std::basic_ostream 객체에 operator<<를 사용해 타입 T의 연속된 객체들을 쓰는 단일 패스 LegacyOutputIterator예요. C++11부터 있어요.

출처: cppreference

본문

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

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

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

std::ostream_iteratoroperator<<로 생성된 std::basic_ostream 객체에 타입 T의 연속된 객체들을 쓰는 단일 패스 LegacyOutputIterator예요. 선택적 구분자 문자열이 매 쓰기 연산 뒤 출력 스트림에 쓰여요. 쓰기 연산은 반복자(역참조 여부와 무관)에 대입할 때 수행돼요. std::ostream_iterator를 증가시키는 것은 no-op이에요. 전형적인 구현에서 std::ostream_iterator의 유일한 데이터 멤버는 연관된 std::basic_ostream에 대한 포인터예요.

멤버 타입

멤버 타입 정의
iterator_category std::output_iterator_tag
value_type void
difference_type void / std::ptrdiff_t(C++20)
pointer void
reference void
char_type CharT
traits_type Traits
ostream_type std::basic_ostream<CharT, Traits>

멤버 함수

  • (constructor) — 출력 반복자를 구성해요(구분자 선택).
  • operator= — 출력 스트림에 값을 써요(*os << value, 있으면 구분자).
  • operator*, operator++, operator++(int) — no-op.

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3};
    std::copy(v.begin(), v.end(),
              std::ostream_iterator<int>(std::cout, ", "));
    // "1, 2, 3, "
}

더 알아보기 (Learn more)

cppreference