std :: ostreambuf_iterator和std :: cout不能一起使用

时间:2020-04-06 16:30:51

标签: c++ c++11

我正在尝试编写代码以了解std :: ostreambuf_iterator的用法:

#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <sstream>

int main()
{
    std::cout << "Hello Boy";
    std::ostreambuf_iterator<char> s2(std::cout);
    std::cout << " s2 = " << *s2 << std::endl;
}

但是编译失败,指出“没有运算符<<匹配操作数”-我们不能在ostreambuf_iterator上使用<<吗?那么我们如何打印迭代器保存的值?

1 个答案:

答案 0 :(得分:2)

您无法从输出流中读取内容,因此输出流迭代器无法从operator*返回任何有意义的内容。

那我们如何打印迭代器保存的值?

迭代器不保存任何数据。它将写入调用operator=时提供给它的流。