How to write vector values to a file

前端 未结 4 766
闹比i
闹比i 2020-12-23 17:13

I have a large vector.

The ways that I use multiply the run-time of the program hugely. The first is write all values to a string as they are calculated using

4条回答
  •  时光取名叫无心
    2020-12-23 18:02

    Maybe I have missed something, but what is wrong with:

    std::ofstream f("somefile.txt");
    for(vector::const_iterator i = v.begin(); i != v.end(); ++i) {
        f << *i << '\n';
    }
    

    That avoids having to do potentially quadratic string concatenation, which I assume is what's killing your run-time.

提交回复
热议问题