2017-05-14 2 views
-1

지도에 추가 한 typedef struct 변수에서 speed의 첫 번째 값을 가져 오려고합니다. 지금은 내 코드가 내가 읽은 CSV 파일의 모든 값을 반환하고 있습니다. 전체 열의 최대 값을 얻기 위해 비교를 수행하는 첫 번째 값만 반환하도록하려면 어떻게해야합니까?지도에서 첫 번째 값 가져 오기

map.begin()->((*it).second).speed)을 사용해 보았지만 작동하지 않습니다.

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Date.h" 
#include "Time.h" 
#include <stdlib.h> 

#include <map> 


using namespace std; 

typedef struct 
{ 

Time t; 
float speed; 
int solar; 

} 
WindLogType; 

date d; 
multimap<date, WindLogType> windlog; 
ifstream input , csv; 
ofstream output; 
string filename; 
int number,choice; 
string *filelist = NULL; 
WindLogType T1; 


int main() 
{ 
output.open("data/met_index.txt"); 

cout << "Enter number of file to read : " << endl; 
cin >> number ; 

for(int i =0; i< number ; i++) 
{ 
    cout << "Enter File name : " << endl; 
    cin >> filename; 
    output << filename << endl; 
} 
filelist = new string[number]; 

output.close(); 

input.open("data/met_index.txt", ios::in); 
if(!input.is_open()) 
{ 
    cout<< "File not found."<<endl; 
    return 0; 
} 
else 
{ 
    string line, line2; 
    while(getline(input, line, '\n')) 
    { 
     //cout << line << endl; 
     line = "data/" + line; 
     for(int i =0; i<number; i++) 
     { 
      filelist[i] = line; 
      cout << filelist[i]; 
      csv.open(filelist[i].c_str()); 

      string line,sDay, sMonth, sYear, sHH, sMM; 

      while(getline(csv,line2, '\n')) 
      { 
       //cout << line2 << endl; 
       getline(csv, sDay,'/'); 
       getline(csv, sMonth,'/'); 
       getline(csv, sYear,' '); 
       getline(csv, sHH,':'); 
       getline(csv, sMM,','); 
       int day1 = atoi(sDay.c_str()); 
       int month1 = atoi(sMonth.c_str()); 
       int year1 = atoi(sYear.c_str()); 
       int hour1 = atoi(sHH.c_str()); 
       int min1 = atoi(sMM.c_str()); 

       float s1 = 0.0; 
       int sr = 0; 
       for (int i=0; i<10; i++) 
       { 
        csv >> s1; 
        csv.ignore(100, ','); 
       } 
       for(int j =0; j<18; j++) 
       { 
        csv >> sr; 
        csv.ignore(50,','); 
       } 

       T1.t.setTime(hour1, min1); 
       T1.speed = s1; 
       T1.solar = sr; 

       d.setDate(day1, month1, year1); 
       windlog.insert(pair<date, WindLogType>(d, T1)); 

       multimap<date, WindLogType> :: iterator it; 
       for(it =windlog.begin(); it!= windlog.end(); ++it) 
       { 
        int max_value = ((*it).second).speed; 
        if((*it).second.speed > max_value){ 
         max_value = ((*it).second).speed; 
        } 
        cout << max_value<<endl; 
       } 
      } 
      csv.close(); 
     } 
     /**/ 
    } 
    input.close(); 
    input.clear(); 
    //input.open(filelist[0].c_str(), ios::in); 
} 
return 0; 
} 
+0

"시도했지만 작동하지 않습니다"라고 말하면 정확히 어떤 일이 발생합니까? 다른 말로하면, 당신은 무엇을 기대 했습니까? 그리고 당신이 보는 것이 당신이 기대하는 것과 어떻게 다릅니 까? –

+0

그것은 나에게 오류를 던졌습니다 ... 나는 단지 전체 열에서 하나의 최대 값을 볼 수 있어야합니다. –

+0

게시물에 오류를 쓰고 [mcve] –

답변

1

매번 max_value이 인쇄됩니다.

예를 들어 csv.close() 뒤에 모든 것을 삽입 한 후 최대 값을 찾은 행을 이동하십시오. 또한 검색하는 동안 최대 요소를 인쇄하지 말고 모든 요소를 ​​반복합니다.

multimap<date, WindLogType> :: iterator it =windlog.begin(); 
int max_value = ((*it).second).speed; 
for(++it ; it!= windlog.end(); ++it) 
{ 
    if((*it).second.speed > max_value){ 
     max_value = ((*it).second).speed; 
    } 
} 
cout << max_value<<endl; 

물론지도가 비어 있지 않아야합니다.


편집

WindLogType.speed 부동이고 최대를 찾을 때 당신은 너무 떠되어야 정수를 사용하고 있습니다. 아마도 이미 알고 있을지 모르지만 C++ 11 이후에는 컴파일러에서 할당 표현식에 따라 올바른 유형을 자동으로 추론하도록 auto 지정자를 사용할 수 있습니다. Visual Studio 2010 및 gcc 4.4부터 사용할 수 있습니다 (gcc의 경우 --std=c++11 옵션을 포함해야 함).

if (!windlog.empty()) { 
    auto it = windlog.begin(); // 'it' is an iterator 
    auto max_value = it->second.speed; // you're now sure it uses the same type 
    for(++it; it!= windlog.end(); ++it) { 
    max_value = std::max(it->second.speed, max_value); 
    } 
    std::cout << max_value << std::endl; 
} else { 
    std::cout << "Empty map" << std::endl; 
} 
관련 문제