ファイルからの読み取りのパフォーマンスを向上させるために、大きな (数 MB) ファイルの内容全体をメモリに読み込んでから、istringstream を使用して情報にアクセスしようとしています。
私の質問は、この情報を読み取って文字列ストリームに「インポート」する最良の方法はどれですか? このアプローチの問題 (以下を参照) は、文字列ストリームを作成するときにバッファがコピーされ、メモリ使用量が 2 倍になることです。
#include <fstream>
#include <sstream>
using namespace std;
int main() {
ifstream is;
is.open (sFilename.c_str(), ios::binary );
// get length of file:
is.seekg (0, std::ios::end);
long length = is.tellg();
is.seekg (0, std::ios::beg);
// allocate memory:
char *buffer = new char [length];
// read data as a block:
is.read (buffer,length);
// create string stream of memory contents
// NOTE: this ends up copying the buffer!!!
istringstream iss( string( buffer ) );
// delete temporary buffer
delete [] buffer;
// close filestream
is.close();
/* ==================================
* Use iss to access data
*/
}