C++ binary file I/O to/from containers (other than char *) using STL algorithms

前端 未结 4 548
逝去的感伤
逝去的感伤 2021-01-06 22:52

I\'m attempting a simple test of binary file I/O using the STL copy algorithm to copy data to/from containers and a binary file. See below:

 1 #include 

        
4条回答
  •  萌比男神i
    2021-01-06 23:37

    You could set the precision using setprecision as Tristram pointed out, and do you need a delimiter. See the cppreference to see how the operator= functions. There is no format set, so you will need to set it on output:

    ofstream output ("temp.bin", ios::binary);
    output.flags(ios_base::fixed);  //or output << fixed;
    copy(vd.begin(), vd.end(), oi_t(output, " "));
    output.close();
    

    I would tend to favor using fixed to eliminate precision problems. There have been many cases were someone thought "we'll never need more than 5 digits" so they hardcoded a precision everywhere. Those are costly bugs to have to correct.

提交回复
热议问题