这是家庭作业。因此,无论如何,该练习要求用户读取csv文件,通过Template Vector类使用内部数据(风速)进行计算,并显示平均风速以及其他一些信息。问题在于如何将文件读入向量类以执行计算。
我一直不成功,使用getline和stringstream隔离特定值(在10个逗号之后)。问题是当我尝试将值与Template Vector类一起使用时。诸如push_back()和size()之类的某些功能有效,但是at()和print()却遇到问题。
// included all the necessary .h and <> here
typedef struct
{
Date d;
Time t;
float speed;
} WindLogType;
// declare speed max function (I just realised I have no idea what this was for)
ostream & operator << (ostream & osObject, const WindLogType & W1);
istream & operator >> (istream & input, WindLogType & W1);
int main()
{
// Declared a lot of variables here etc.
Vector <WindLogType> windlog;
WindLogType transfer; // Declaring an object of WindLogType
while(getline(input, line))
{
stringstream line2(line);
string cell; // Denotes a particular column
int col = 0;
while(getline(line2, cell, ','))
{
if(col == 10)
{
speed = atof(cell.c_str()); // Converting to c-string, and
// then float
transfer.speed = speed; // Inputting wind speed into
// WindLogType speed
windlog.push_back(transfer);
}
col++;
}
}
// Sum of all the wind speeds
for(int i = 0; i < windlog.size(); i++)
{
sum = sum + (windlog.at(i) * 3.6); // Converting every elements to
// km/h
}
average = sum/windlog.size(); // Average wind speed
cout << average;
while(input.eof())
{
...;
}
// Printing out date and time of speeds that be equal to the average
for(int j = 0; j < windlog.size(); j++)
{
if(windlog.at(j) == average)
{
...;
}
}
input.close();
}
// In the Template Vector Class
template <class Type>
class Vector
{
Type *list;
int length; // The number of arrays of a current vector.
void push_back(const Type& i);
...; // Everything else
}
template <class Type>
Type& Vector<Type>::at(int i)
{
if((i >= 0) && (i < length))
{
return list[i];
}
return list[0];
}
我希望sum = sum + (windlog.at(i) * 3.6)
或if(windlog.at(j) == average)
可以正常工作,但是它返回error: no match for 'operator*' (operand types are 'WindLogType' and 'double')
。
那么我是如何将csv文件读入向量的呢?