【发布时间】:2013-11-05 07:31:24
【问题描述】:
我正在尝试将存储在不同行中的未知数量的双精度值从一个文本文件读取到一个名为rainfall 的向量中。我的代码无法编译;对于 while 循环行,我收到错误 no match for 'operator>>' in 'inputFile >> rainfall'。我了解如何从文件读入数组,但我们需要为这个项目使用向量,但我不明白。我很感激你能给我下面的部分代码的任何提示。
vector<double> rainfall; // a vector to hold rainfall data
// open file
ifstream inputFile("/home/shared/data4.txt");
// test file open
if (inputFile) {
int count = 0; // count number of items in the file
// read the elements in the file into a vector
while ( inputFile >> rainfall ) {
rainfall.push_back(count);
++count;
}
// close the file
【问题讨论】:
-
我建议使用迭代器对构造函数。网上有很多例子。