std::ostreambuf_iterator<CharT,Traits>::ostreambuf_iterator
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
ostreambuf_iterator( streambuf_type* buffer ) throw(); |
(C++11 前) | |
ostreambuf_iterator( streambuf_type* buffer ) noexcept; |
(C++11 起) | |
| (2) | ||
ostreambuf_iterator( ostream_type& stream ) throw(); |
(C++11 前) | |
ostreambuf_iterator( ostream_type& stream ) noexcept; |
(C++11 起) | |
2) 同
ostreambuf_iterator(stream.rdbuf())。参数
| stream | - | 拥有的 rdbuf() 会被此迭代器访问的输出流
|
| buffer | - | 此迭代器要访问的输出流缓冲 |
示例
运行此代码
#include <fstream>
#include <iostream>
#include <iterator>
int main()
{
const char* file = "test.txt";
{
std::basic_filebuf<char> f;
f.open(file, std::ios::out);
std::ostreambuf_iterator<char> out1(&f);
*out1 = 'a'; // 通过迭代器写入文件
}
// 从文件读回
char a;
std::cout << ((std::ifstream{file} >> a), a) << std::endl;
std::ostreambuf_iterator<wchar_t> out2{std::wcout};
*out2 = L'b';
}
输出:
a
b
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 112 | C++98 | 要求"实参不能为空"应用到了重载 (2) | 改为应用到重载 (1) |
| P2325R3 | C++20 | 因为 C++20 迭代器必须是 default_initializable 的而提供了默认构造函数
|
与该要求一同移除 |