简体   繁体   中英

How to read and write large std::vector<std::vector<float>> to file in C++

In the constructor of my program I am generating a very large lookup table of 2048 * 2048 floats using nested std::vector<std::vector<float>> .

The lookup table is always the same each time, so I'd like to write this table to a file to save recalculating it.

What is the best way to achieve this? Is it best to write the values to a big header, or is it wiser to save a binary copy of the object?

Thanks in advance!

Edit:

If it is of any conceqence, perhaps it's related to the time take to allocate the memory which is being done with the following pattern:

dataStructure.resize(numberOfRows);
for (int i = 0; i < numberOfRows; i++)
     dataStructure[i].resize(numberOfColumns);
    

Where datastructure is a std::vector<std::vector<float>>

The answer in this case has been to optimise the lookup table generation. I was doing much, much more work than was required, and now load times are practically instant.

For those in the future looking at this thread with a similar problem, there is great general advice in the commments.

Thanks to the attention of all who contributed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM