std::ostream_iterator
定义于头文件
<iterator> |
||
template< class T,
class CharT = char, |
(C++17 前) | |
template< class T,
class CharT = char, |
(C++17 起) | |
std::ostream_iterator
是单趟老式输出迭代器 (LegacyOutputIterator) ,用 operator<<
写入相继 T
类型对象到为之创建迭代器的 std::basic_ostream 对象。每次写操作后写入可选的分隔字符串。写操作在赋值给迭代器时(无论是否解引用)进行。自增 std::ostream_iterator
是无操作。
典型实现中, std::ostream_iterator
仅有的数据成员是指向关联 std::basic_ostream
的指针和指向分隔字符串首字符的指针。
写入字符时, std::ostreambuf_iterator 更有效率,因为它避免对每个字符构造并析构一次 sentry 对象的开销。
成员类型
成员类型 | 定义 | ||||
iterator_category |
std::output_iterator_tag | ||||
value_type |
void | ||||
difference_type |
|
||||
pointer |
void | ||||
reference |
void | ||||
char_type |
CharT |
||||
traits_type |
Traits |
||||
ostream_type |
std::basic_ostream<CharT, Traits> |
要求通过从 std::iterator<std::output_iterator_tag, void, void, void, void> 继承获得成员类型 |
(C++17 前) |
成员函数
构造新的 ostream_iterator (公开成员函数) |
|
析构 ostream_iterator (公开成员函数) |
|
写对象到关联的输出序列 (公开成员函数) |
|
无操作 (公开成员函数) |
|
无操作 (公开成员函数) |
示例
#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
参阅
写入 std::basic_streambuf 的输出迭代器 (类模板) |
|
从 std::basic_istream 读取的输入迭代器 (类模板) |
|
(库基础 TS v2)
|
写入相继元素到输出流的输出迭代器,以分割器分隔相邻元素 (类模板) |