如何直接读取一大块内存到std :: vector? [英] How to directly read a huge chunk of memory into std::vector?
问题描述
我有一个巨大的连续数组 x
,我从文件
I have a huge contiguous array x
that I fread
from a file.
如何将这个块放入 std :: vector<>
?换句话说,我喜欢的结果是在 std :: vector<>
而不是数组,但我想要的结果C ++代码是一样高效的
How do I drop this chunk into a std::vector<>
? In other words, I prefer to have the result to be in std::vector<>
rather than the array, but I want the resultant C++ code to be as efficient as this plain C-version which drops the chunk right into the array.
通过搜索,我想我可能需要在某种形式中使用placement-new, m不确定呼叫和所有权问题的顺序。
From searching around, I think I may have to use placement-new in some form, but I'm uncertain about the sequence of calls and ownership issues. Also, do I need to worry about alignment issues?
我使用 T = unsigned
进行测试,但我需要担心对齐问题吗?预期一个合理的解决方案适用于任何POD结构。
I am testing for with T = unsigned
, but I expect a reasonable solution to work for any POD struct.
using T = unsigned;
FILE* fp = fopen( outfile.c_str(), "r" );
T* x = new T[big_n];
fread( x, sizeof(T), big_n, fp );
// how do I get x into std::vector<T> v
// without calling a gazillion push_backs() or copies ?!?
delete[] x;
fclose( fp );
推荐答案
使用 std: :vector
构造函数,用于设置向量,并使用 std :: vector :: data
以获取指向已分配内存的指针。
You use the std::vector
constructor which sets the size of the vector, and use std::vector::data
to get a pointer to allocated memory.
使用 fread
:
std::vector<T> x(big_n);
fread(x.data(), sizeof(T), big_n, fp);
正如其他人所说,使用 fread
如果类型 T
不是 POD类型很可能不工作。然后,您可以使用C ++流和 std :: istreambuf_iterator
将文件读入向量。但是,这有一个缺点,它循环文件中的所有项目,如果 big_n
大到听起来那么这可能是一个性能问题。
As noted by others, using fread
if the type T
is not a POD type will most likely not work. You can then use C++ streams and std::istreambuf_iterator
to read the file into the vector. However this have the drawback that it loops over all items in the file, and if big_n
is as big as it sounds then this might be a performance problem.
但是,如果文件真的很大,我建议使用内存映射来读取文件。
However, if the file truly is big, I rather recommend using memory mapping to read the file.
这篇关于如何直接读取一大块内存到std :: vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!