Fastest way to write large STL vector to file using STL

后端 未结 7 707
一个人的身影
一个人的身影 2021-02-06 06:53

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:

ve         


        
7条回答
  •  醉梦人生
    2021-02-06 07:04

    With such a large amount of data to be written (~1GB), you should write to the output stream directly, rather than using an output iterator. Since the data in a vector is stored contiguously, this will work and should be much faster.

    ofstream outfile("nanocube.txt", ios::out | ios::binary);
    outfile.write(&vs[0], vs.size());
    

提交回复
热议问题