gpt4 book ai didi

c++ - 指向移位数组 C++ 的指针

转载 作者:行者123 更新时间:2023-11-27 23:34:40 26 4
gpt4 key购买 nike

我试图尽可能有效地向左移动我的数组。我现在使用指针,并且无法将值分配回我的数组:

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

}
}

最佳答案

c++ 在<algorithm> 中内置了一个函数.只需调用 std::rotate(front_ptr, front_ptr + N, back_ptr);

关于c++ - 指向移位数组 C++ 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1656329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com