2016-04-05 166 views
-1

需要您的幫助,如果您能夠快速幫助我,請更好。這是一個非常微不足道的問題,但仍然無法理解我究竟需要將其放在一行中。增加地圖的價值

下面的代碼,我有

for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++) 
{ 
    double Min = DBL_MAX; 
    int station = 0; 

    for (int i = 0; i < newStations; i++) 
    { 

     distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2)); 
     if (distance < Min) 
     { 
      Min = distance; 
      station = i; 

     } 
    } 

    if (people.find(station) == people.end()) 
    { 
     people.insert(pair<int, int>(station, i)); 
    } 

    else 
    { 
     how can i increment "i" if the key of my statation is already in the map. 
    } 

} 

只是簡單我做什麼,我拿第一BUSREQUEST轉到第二圈取第一站,找到的最小距離。在我結束第二個循環之後,我將該站點與我的地圖保持最小距離。我繼續我所有的循環,如果有相同的站,我需要增加它,所以這意味着該站使用兩次等。

我需要幫助只是給我提示或提供線路我需要添加。

我提前感謝您,並等待您的幫助。

+0

YourMap [YourKey] ++ –

+0

謝謝!你幫了我很多! –

+0

:)很高興我幫助。我會把它放在一個答案中,請將其標記爲答案。 –

回答

1

我想你的意思是Min Distance而不是i?檢查並讓我知道。

for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++) 
{ 
    double Min = DBL_MAX; 
    int station = 0; 

    for (int i = 0; i < newStations; i++) 
    { 

     distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2)); 
     if (distance < Min) 
     { 
      Min = distance; 
      station = i; 

     } 
    } 

    if (people.find(station) == people.end()) 
    { 
     people.insert(pair<int, int>(station, i)); // here??? 
    } 

    else 
    { 
     // This routine will increment the value if the key already exists. If it doesn't exist it will create it for you 
     YourMap[YourKey]++; 
    } 

} 
+0

第一步,請告訴我,如果我對循環使用後(自動它= people.begin(!);它= people.end();它++) \t { \t \t COUT << it->第一<< ";" << it->秒<< ENDL; \t}如果id 0有站號,這個id是否與我之前使用的站相對應? –

+0

@mishaOstapchuk編輯您的原始帖子。把新的代碼,新的問題,新的錯誤(如果有的話),我會在這裏回答。遵循這種模式堆棧溢出 –

+0

我發佈在最後,我知道這是超出規則,但我今天不能發佈問題。 –

0

在C++中,您可以直接訪問映射鍵而不插入它。 C++會自動使用默認值創建它。 在你的情況下,如果一個station不在people地圖中,你將訪問people[station]然後people[station]將自動設置爲0(默認值爲int爲0)。

所以你可以這樣做:

if (people[station] == 0) 
{ 
    // Do something 
    people[station] = station; // NOTE: i is not accessible here! check ur logic 
} 
else 
{ 
    people[station]++; 
} 


另外:在你的代碼i不能訪問內部IF插入狀態進入人們映射。

+0

我在第一次循環之後添加了int i = 1以及我使用Min = dbl_max。而我的鑰匙應該對應於有最小距離的車站。這意味着如果我有站號75,如果這個站再次出現在我的地圖上,它應該增加 –

+0

否,你沒有。至少在我的代碼中看不到。 –

+0

是的,我沒有發送代碼。對不起 –