c++ - 为什么 istream_iterator<unsigned char, unsigned char> 抛出 std::bad_cast?

标签 c++ stl iostream istream istream-iterator

这是怎么回事?

#include <iostream>
#include <iterator>
#include <sstream>

int main() {
    std::basic_stringbuf<unsigned char> buf;
    std::basic_istream<unsigned char> stream(&buf);
    // the next line throws std::bad_cast on g++ 4.4
    std::istream_iterator<unsigned char, unsigned char> it(stream);
}

在构造迭代器之前,我尝试了 stream.write(some_array, sizeof(some_array),但无济于事。

谢谢。

最佳答案

它从 sentry 对象的构造函数中抛出,它检查流上的 ctype 面(它需要它以便它可以跳过空格),它恰好为 NULL,因为它没有为无符号字符定义。

您需要处理该流上的空格吗?如果不是,则更改为

std::istreambuf_iterator<unsigned char> it(stream);

关于c++ - 为什么 istream_iterator<unsigned char, unsigned char> 抛出 std::bad_cast?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3610000/

相关文章:

c++ - std::cin 在不应该接收输入时接收输入

C# - StreamReader/StreamWriter - 另一个进程使用的文件

c++ - 为什么一个子线程的执行时间比整个应用程序的执行时间多

c++ - 如何使用 OpenGL 将数据发送到顶点着色器?

c++ - 如何轻松检查 std::map 和 std::unordered_map 是否包含相同的元素

c++ - 在 Visual Studio 2015 中的 C++ STL 中出现错误

c++ - 从逗号分隔的字符串中解析整数

c++ - "passing const XXX as this disregards qualifiers"的解释?

c++ - 这是在字符串为空时执行 strcmp 以返回 false 的好方法吗

C++ STL 内存管理 : Stack or Heap?