2013-04-03 46 views
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  

這是哪裏IM達對目前的代碼:

#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; 
} 
+3

什麼問題你有? – jrok

+3

究竟是什麼問題? – crashmstr

+0

重用?矢量?結構中的條目?還有別的嗎? –

回答

1

既然你沒有告訴我們什麼是您所遇到的問題,我只能猜測。我注意到你正在讀取int變量中的所有內容。

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

您的數據的格式建議你需要一個浮點數據類型(tmaxtminrain至少)。

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 
} 
+0

就像浮動tmax,tmin,rain一樣簡單; – jaylad

+0

@jay是的,就這麼簡單。 – jrok

0

首先,你需要確保你跳過第2行,因爲它們不是數值。另外,如另一個人所說,確保您讀取double的值,您想要存儲的值爲doubleint,您想要讀取的值爲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 
+0

謝謝你的最後一個問題 – jaylad

+0

,在文件的某些月份有更多的數據缺失標記爲「 - 」,我如何解釋這一點,我是否也需要做一些形式的錯誤檢查? – jaylad

+0

您可以將每個值存儲爲'string',然後如果您想將其用作數值,則可以將其轉換爲'double'或'int'。此外,所有的輸入文件可能有幾個月或其他值標記爲 - ? – OGH

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

0

你已經做到了這一點。有這樣做,就像第一次創建結構,數據填充它的其他方式,推動認爲​​:

Weather weather; 

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

data_weather.push_back(weather); 
+0

謝謝,我怎麼能呼喚出幾個月的價值數據說tmax?並用它來做一個計算 – jaylad

+0

@jaylawson你可以使用例如['std :: copy_if'](http://en.cppreference.com/w/cpp/algorithm/copy)與一個特殊的謂詞複製特定的條目到一個新的向量。 –

相關問題