2009-11-01 110 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