가능한 한 효율적으로 배열을 왼쪽으로 시프트하려고합니다. 임 지금 포인터를 사용하고, 메신저 내 배열로 다시 값을 할당하는 데 문제 : <algorithm>
에 내장하는 기능을 가지고 ++배열을 시프트 할 포인터 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
}
}
}
}
21 % 이상 응답을 받으면 도움을 얻을 수 있습니다. – cdiggins
그리고 그가 똑같은 질문을 반복해서 반복하지 않았다면 ... –
예를 들어, "top"이 정의 된 곳 ... "n"은 무엇입니까? 항목 주소를 참조하는 – dicroce