命名空间
变体
操作

std::ostreambuf_iterator

来自 cppreference.cn
< cpp‎ | iterator
 
 
迭代器库
迭代器概念
迭代器原语
算法概念和工具
间接可调用概念
常用算法要求
(C++20)
(C++20)
(C++20)
实用工具
(C++20)
迭代器适配器
范围访问
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
定义于头文件 <iterator>
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;
(自 C++17 起)

std::ostreambuf_iterator 是一个单趟 LegacyOutputIterator,它将连续字符写入为其构造的 std::basic_streambuf 对象中。实际的写入操作在迭代器(无论是否解引用)被赋值时执行。递增 std::ostreambuf_iterator 是空操作。

在一个典型的实现中,std::ostreambuf_iterator 的唯一数据成员是指向关联的 std::basic_streambuf 的指针和一个布尔标志,指示是否已达到文件结束条件。

目录

[编辑] 成员类型

成员类型 定义
iterator_category std::output_iterator_tag
value_type void
difference_type

void

(直到 C++20)

std::ptrdiff_t

(自 C++20 起)
pointer void
reference void
char_type CharT
traits_type Traits
streambuf_type std::basic_streambuf<CharT, Traits>
ostream_type std::basic_ostream<CharT, Traits>

成员类型 iterator_categoryvalue_typedifference_typepointerreference 需要通过继承自 std::iterator<std::output_iterator_tag, void, void, void, void> 获取。

(直到 C++17)

[编辑] 成员函数

构造一个新的 ostreambuf_iterator
(公有成员函数) [编辑]
(析构函数)
(隐式声明)
析构一个 ostreambuf_iterator
(公有成员函数) [编辑]
向关联的输出序列写入一个字符
(公有成员函数) [编辑]
空操作
(公有成员函数) [编辑]
空操作
(公有成员函数) [编辑]
测试输出是否失败
(公有成员函数) [编辑]

[编辑] 示例

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
 
int main()
{
    std::string s = "This is an example\n";
    std::copy(s.begin(), s.end(), std::ostreambuf_iterator<char>(std::cout));
}

输出

This is an example

[编辑] 参见

std::basic_streambuf 读取的输入迭代器
(类模板) [编辑]
写入 std::basic_ostream 的输出迭代器
(类模板) [编辑]