2
如何有效地將數據從一個數據流插入另一個不同類型的數據流?在不同類型的數據流之間複製數據
我曾嘗試以下:
#include <iostream>
#include <sstream>
using namespace std;
int main(void)
{
basic_stringstream<unsigned short> uss;
stringstream cs;
unsigned short val = 0xffff;
uss.write(&val, 1); // write value to 'uss'
uss.read(&val, 1); // read data from 'uss' into 'val'
cout << hex << val << endl; // gives 0xffff
cs << uss.rdbuf(); // copy 'uss' contents into 'cs'
cs.read((char*) &val, 2); // read data from 'cs' into 'val'
cout << hex << val << endl; // gives 0x3030 ?
return 0;
}
http://stackoverflow.com/questions/5497284/is-it-ok-to-use-iostreams-with-int-as-character-type – 2014-08-31 23:45:15
'cs << uss.rdbuf();'This line調用'operator <<(void *)',並在十六進制中打印指針,就像使用'printf(「%p」)''一樣。我假設'rdbuf()'指針的地址是以十六進制呈現的形式'00XXXXXX',因此是0x3030(又名「00」)。 – 2014-08-31 23:54:33