2009-11-01 109 views
0

我試圖儘可能有效地移動我的數組。即時通訊使用指針,現在,有麻煩分配值回我的數組IM:指針移位數組C++

void stack::rotate(int nRotations) 
{ 
    if (count <= 1) return; 

    int *intFrontPtr = &items[top+1].n; 
    int *intBackPtr = &items[count-1].n; 
    int temp = 0; 

    for (int shift = 0; nRotations != 0 ;) 
    { 
     if (nRotations > 0) // we rotate left 
     { 
      temp = *++intFrontPtr; // give temp the value 
      items[++shift].n = temp; // debug shows success 
      if (shift == count) // dont overrun array 
      { 
       temp = *intBackPtr; 
       items[count-1].n = temp; 
       shift = 0; // reset for another rotation 
       nRotations--; // decrement we have reached the end 
      } 
     } 

    } 
} 
+1

如果您接受的答案超過21%,那麼您可能會獲得更多幫助。 – cdiggins 2009-11-01 04:23:48

+0

而且,如果他不只是一遍又一遍地重複同一個問題... – 2009-11-01 14:03:38

+0

太多沒有給出答案...例如,「top」定義在哪裏......什麼是「n」? – dicroce 2009-11-01 16:49:03

回答

4

C++有建在<algorithm>的功能。只需撥打std::rotate(front_ptr, front_ptr + N, back_ptr);

+0

指的是項目地址可能是問題? – user40120 2009-11-01 03:57:26

+0

我需要解決這個問題..爲什麼不通過價值轉移? – user40120 2009-11-01 04:02:48