【发布时间】:2014-12-22 17:38:45
【问题描述】:
我阅读了an answer here,它展示了如何使用以下一(二)个衬垫将整个流读入 std::string:
std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(stream), eos);
对于类似将二进制流读入std::vector 的操作,我为什么不能简单地将char 替换为uint8_t 并将std::string 替换为std::vector?
auto stream = std::ifstream(path, std::ios::in | std::ios::binary);
auto eos = std::istreambuf_iterator<uint8_t>();
auto buffer = std::vector<uint8_t>(std::istreambuf_iterator<uint8_t>(stream), eos);
以上产生编译器错误(VC2013):
1>d:\non-svn\c++\library\i\file\filereader.cpp(62): 错误 C2440: '' : 不能从 'std::basic_ifstream>' 到 'std::istreambuf_iterator>' 1>
与 1> [ 1> _Elem=uint8_t 1> ] 1>
没有构造函数可以采用源类型或构造函数重载 分辨率不明确
【问题讨论】:
-
根据错误消息,
char和uint8_t在您的编译器上不是一回事。尝试改用char。 -
@cdhowie
uint8_t是unsigned char,所以是的,在任何计算机上都不一样 ;) 但是,这可能是一个模棱两可的演员阵容,因为ifstream的输出是char. -
是的,它适用于 char 但 uint8_t 无论如何都是 unsigned char。
-
@Robinson:没错。
char和unsigned char是不同的类型。 -
@cdhowie
char、signed char和unsigned char始终是 3 种不同的类型。
标签: c++ vector stl stream ifstream