我是C++中的新成员,并且难以从文件导入特定数据(数字)。 我的输入是这样的:使用Vector函数从文件中读取特定数据C++
Open High Low Close
1.11476 1.11709 1.10426 1.10533
1.10532 1.11212 1.10321 1.10836
1.10834 1.11177 1.10649 1.11139
1.09946 1.10955 1.09691 1.10556
1.10757 1.11254 1.09914 1.10361
1.10359 1.12162 1.10301 1.11595
1.09995 1.10851 1.09652 1.10097
我用下面的代码对我来说完全读取第二列的正常工作,但是我需要为只读的特定数据。例如第三行/第三列是1.10649
如何读取特定数据?我是否需要使用字符串获取行/列,然后将其转换为int以便在向量中读取它?如果有任何建议可以帮助我解决这个问题,我将不胜感激。
// Data import 2nd Column
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
const int columns = 4;
vector< vector <double> > data;
ifstream market_data("market_data.txt");
if (market_data.is_open()) {
double num;
vector <double> line;
while (market_data >> num) {
line.push_back(num);
if (line.size() == columns) {
data.push_back(line);
line.clear();
}
}
}
vector <double> column;
double col = 2;
for (double i = 0; i < data.size(); ++i) {
column.push_back(data[i][col - 1]);
cout << column[i] << endl;
}
system ("pause");
return 0;
}
为什么不更改为“size_t是精确”(您是否让编译器发出签名/未签名警告)? –
@DieterLücking好主意,修正。 –
@πάνταῥεῖ谢谢你的回复,但通过改变size_t并没有解决问题,输出仍然是相同的..我需要的是获得所有数据中的1.10649输出 – Tinto