我正在尝试编写代码以了解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上使用<<吗?那么我们如何打印迭代器保存的值?
答案 0 :(得分:2)
您无法从输出流中读取内容,因此输出流迭代器无法从operator*
返回任何有意义的内容。
那我们如何打印迭代器保存的值?
迭代器不保存任何数据。它将写入调用operator=
时提供给它的流。