使用STL将大STL向量写入文件的最快方式 [英] Fastest way to write large STL vector to file using STL

查看:234
本文介绍了使用STL将大STL向量写入文件的最快方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的向量(10 ^ 9元素)的字符,我想知道什么是最快的方式来写这样的向量到一个文件。到目前为止我一直使用下面的代码:

I have a large vector (10^9 elements) of chars, and I was wondering what is the fastest way to write such vector to a file. So far I've been using next code:

vector<char> vs;
// ... Fill vector with data
ofstream outfile("nanocube.txt", ios::out | ios::binary);
ostream_iterator<char> oi(outfile, '\0');
copy(vs.begin(), vs.end(), oi);

对于此代码,将所有数据写入文件大约需要两分钟。实际的问题是:我可以使用STL和如何使用它更快。

For this code it takes approximately two minutes to write all data to file. The actual question is: "Can I make it faster using STL and how"?

推荐答案

第二个参数 ostream_iterator 的构造函数。它应该是NULL指针,如果你不想要一个分隔符(虽然,幸运的是,这将被视为隐式),或第二个参数应该省略。

There is a slight conceptual error with your second argument to ostream_iterator's constructor. It should be NULL pointer, if you don't want a delimiter (although, luckily for you, this will be treated as such implicitly), or the second argument should be omitted.

但是,这意味着在写每个字符后,代码需要检查指定分隔符的指针(这可能有点低效)。

However, this means that after writing each character, the code needs to check for the pointer designating the delimiter (which might be somewhat inefficient).

如果你想使用迭代器,也许你可以尝试 ostreambuf_iterator

I think, if you want to go with iterators, perhaps you could try ostreambuf_iterator.

其他选项可能包括使用write )方法(如果它可以处理这么大的输出,或者可能在块中输出),以及可能特定于OS的输出函数。

Other options might include using the write() method (if it can handle output this large, or perhaps output it in chunks), and perhaps OS-specific output functions.

这篇关于使用STL将大STL向量写入文件的最快方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆