我試圖將兩列數據x和y放入兩個向量中,一個只包含x,另一個只包含y。沒有列標題。 像這樣:C++:將文件(未知大小的數據)輸入到矢量問題
x1 y1
x2 y2
x3 y3
然而,當我運行此代碼,我遇到了一個錯誤:(LLDB) 誰能告訴我,如果我做錯了什麼?
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
using namespace std;
int main() {
vector <double> x;
vector <double> y;
ifstream fileIn;
fileIn.open("data.txt"); //note this program should work for a file in the above specified format of two columns x and y, of any length.
double number;
int i=0;
while (!fileIn.eof())
{
fileIn >> number;
if (i%2 == 0) //while i = even
{
x.push_back(number);
}
if (i%2 != 0)
{
y.push_back(number);
}
i++;
}
cout << x.size();
cout << y.size();
fileIn.close();
return 0;
}
什麼,具體而言,是你看到的錯誤? – dg99