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.
Keeping with your use of fread
:
std::vector<T> x(big_n);
fread(x.data(), sizeof(T), big_n, fp);
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.