Fastest way to write large STL vector to file using STL

后端 未结 7 837
一个人的身影
一个人的身影 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:10

    If you have other structure this method is still valid.

    For example:

    typedef std::pair STL_Edge;
    vector v;
    
    void write_file(const char * path){
       ofstream outfile(path, ios::out | ios::binary);
       outfile.write((const char *)&v.front(), v.size()*sizeof(STL_Edge));
    }
    
    void read_file(const char * path,int reserveSpaceForEntries){
       ifstream infile(path, ios::in | ios::binary);
       v.resize(reserveSpaceForEntries);
       infile.read((char *)&v.front(), v.size()*sizeof(STL_Edge));
    }
    

提交回复
热议问题