在线文档教程
C++
迭代器 | Iterator

std::ostream_iterator

STD::Ostream[医]迭代器

Defined in header
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>(until C++17)
template< class T, class CharT = char, class Traits = std::char_traits<CharT>> class ostream_iterator;(since C++17)

std::ostream_iterator是一次过OutputIterator,它写入类型的连续对象。T进入std::basic_ostream对象,并使用operator<<可选分隔符字符串在每次写入操作后写入输出流。当迭代器%28(不管是否取消引用)分配给%29时,将执行写入操作。递增std::ostream_iterator是个禁止行动。

在典型的实现中,std::ostream_iterator是指向关联std::basic_ostream和指向分隔符字符串中的第一个字符的指针。

写字的时候,std::ostreambuf_iterator更有效,因为它避免了每个字符构造和销毁哨兵对象一次的开销。

成员类型

Member typeDefinition
char_typeCharT
traits_typeTraits
ostream_typestd::basic_ostream<CharT, Traits>

成员函数

(constructor)constructs a new ostream_iterator (public member function)
(destructor)destructs an ostream_iterator (public member function)
operator=writes a object to the associated output sequence (public member function)
operator*no-op (public member function)
operator++operator++(int)no-op (public member function)

成员类型

Member typeDefinition
value_typevoid
difference_typevoid
pointervoid
referencevoid
iterator_categorystd::output_iterator_tag

注意:在C++17之前,这些成员类型必须通过从std::iterator<std::output_iterator_tag,void,void,void,void>...

二次

#include <iostream> #include <sstream> #include <iterator> #include <numeric> 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, " ") }

二次

产出:

二次

0.1 0.3 0.6 1

二次

另见

ostreambuf_iteratoroutput iterator that writes to std::basic_streambuf (class template)
istream_iteratorinput iterator that reads from std::basic_istream (class template)
std::experimental::ostream_joiner (library fundamentals TS v2)An output iterator that writes successive elements into an output stream, separating adjacent elements with a delimiter (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/iterator/ostream[医]迭代器