How to read a file into vector in C++?

前端 未结 6 1914
执念已碎
执念已碎 2020-12-01 06:34

I need to read from a .data or .txt file containing a new float number on each line into a vector.

I have searched far and wi

6条回答
  •  独厮守ぢ
    2020-12-01 07:22

    Just to expand on juanchopanza's answer a bit...

    for (int i=0; i=((Main.size())-1); i++) {
        cout << Main[i] << '\n';
    }
    

    does this:

    1. Create i and set it to 0.
    2. Set i to Main.size() - 1. Since Main is empty, Main.size() is 0, and i gets set to -1.
    3. Main[-1] is an out-of-bounds access. Kaboom.

提交回复
热议问题