我讀an answer here展示瞭如何用下面的(二)班輪讀取整個流成的std :: string:如何將整個流讀入std :: vector?
std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(stream), eos);
對於做類似的讀取二進制流的東西變成std::vector
,爲什麼不能」我簡單地用uint8_t
和std::string
替換char
與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:\非SVN \ C++ \庫\ I \文件\ filereader.cpp(62):錯誤C2440: '':不能從 轉換 '的std :: basic_ifstream>' 到 '的std :: istreambuf_iterator>' 1>
其中1> [1> _Elem = uint8_t 1> 1>
沒有構造可以採取源類型或構造函數過載 分辨率不明確
'char'和'uint8_t'不是你的編譯器同樣的事情。嘗試使用'char'來代替。 – cdhowie 2014-10-27 14:08:16
@cdhowie'uint8_t'是'unsigned char',所以是的,在任何一臺計算機上都不一樣;)但是,這可能是一個模糊的轉換,因爲'ifstream'的輸出是'char''。 – aruisdante 2014-10-27 14:09:54
是的,它適用於char,但uint8_t無論如何都是unsigned char。 – Robinson 2014-10-27 14:10:00