tags:

views:

164

answers:

1

Hello,

Im trying to shift my array left as efficiently as possible. Im using pointers now, and im having trouble assigning the values back into my array:

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

    } 
}
+4  A: 

c++ has a function built in in <algorithm>. Just call std::rotate(front_ptr, front_ptr + N, back_ptr);

rlbond
referring to the items address might be the problem?
I need to solve this.. why wont the value transfer over?