1

我正在尝试将数据保存到一个数组中,以便再次重复使用,数据采用以下格式并且来自 1964-2013,这只是一个片段,任何帮助都会很棒,干杯。

 a     b    c       d        e       f     

1964   9   20.5     8.8       0    37.4     
1964  10   13.6     4.2       5    77.8     
1964  11   11.8     4.7       3    45.5     
1964  12    7.7     0.1      17    65.1     
1965   1    7.3     0.8      14    74.6     
1965   2    6.5     0.1      13     3.3     

到目前为止,这是我使用代码的地方:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
#include <vector>
#include <string>

struct Weather
{
  int a;
  int b;
  double c;
  double d;
  int e;
  double f;
  double g;
};

int main()
{
  std::vector<Weather> data_weather;
  string line;
  std::ifstream myfile ("weatherdata.txt");
  if (myfile.is_open())
    {
        while ( getline(myfile, line) )
        {

    std::istringstream buffer(line);
    int a_d, b_d, c_d, d_d, e_d, f_d, g_;
    buffer >> a >> b >> c >> d >> e >> f >> g;

    data_weather.push_back(Weather());
    data_weather.back().a = a_d;
    data_weather.back().b = b_d;
    data_weather.back().c = c_d;
    data_weather.back().d = d_d;
    data_weather.back().e = e_d;
    data_weather.back().f = f_d;
    data_weather.back().g = g_d;

    cout << line << endl;

        }
        myfile.close();
    }
    else
        cout << "unable to open file";

        scat::pause("\nPress <ENTER> to end the program.");

    return 0;
}
4

4 回答 4

2

矢量用法示例..

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<double> student_marks(20);

    for (vector<double>::size_type i = 0; i < 20; i++)
    {
        cout << "Enter marks for student #" << i+1 
             << ": " << flush;
        cin >> student_marks[i];
    }
    // ... Do some stuff with the values

return 0;
}

以下可以帮助您更好地理解...

http://www.mochima.com/tutorials/vectors.html

于 2013-04-03T12:46:49.217 回答
1

由于您没有告诉我们您遇到了什么问题,我只能猜测。我注意到您正在阅读int变量中的所有内容。

int year, mm, tmax, tmin, af, rain, sun;
buffer >> year >> mm >> tmax >> tmin >> af >> rain >> sun;

您的数据格式表明您需要浮点数据类型(对于tmaxtmin并且rain至少)。

sun列也不能被解析为int(那里只有减号)。读入sun变量将失败,并且由于您尚未对其进行初始化,因此您将在此行中调用未定义的行为:

data_weather.back().sun = sun; // sun's value is indeterminate, reading it is UB

除非您绝对确定输入操作不会失败(在用户输入的情况下,这实际上永远不会失败)我建议您养成检查成功的习惯:

if (!(buffer >> year >> mm >> tmax >> tmin >> af >> rain >> sun)) {
    // something went wrong
}
于 2013-04-03T12:46:12.587 回答
0

首先,您需要确保跳过前两行,因为它们不是数值。另外,正如另一个人所说,请确保您将 a 读取double为要存储为 a的值double以及int要读取为 的值int

例子:

double value1, value2;
int value3, value4;
stream >> value1 >> value3 >> value 2 >> value 4;

接下来,您可以创建一个Weather对象并将该对象推入向量中:

Weather objName = {year, month, tempmax, tempmin, airfrost, rain, sun};
data_weather.push_back(objName);

vector[index]您可以通过或访问向量的每个元素vector.at(index)。您也可以通过这种方式修改这些值:

data_weather[2].year = newYear;

要打印,您可以遍历向量并打印出值:

for (auto it = data_weather.begin(); it != data_weather.end(); it++) {
  cout << it.year << " " << it.month << endl; //Or any other values you want
于 2013-04-03T12:46:41.293 回答
0

你完成它的方式是有效的。还有其他方法,比如先创建结构,用数据填充它,然后推送:

Weather weather;

buffer >> weather.year >> weather.month
       >> weather.tempmax >> weather.tempmin
       >> weather.airfrost >> weather.rain >> weather.sun;

data_weather.push_back(weather);
于 2013-04-03T12:48:51.713 回答