简体   繁体   English

移位数组C ++中的元素

[英]Shifting elements in array C++

Im wondering why my data in 'intFront' does not stay the same. 我想知道为什么我在'intFront'中的数据不会保持不变。 Im shifting the elements in my array left: 我将数组中的元素向左移动:

void stack::rotate(int nRotations)
{
    for (; nRotations > 0 ;) // Number of Rotations to the left
    {
        intFront = &items[top+1].n; 
        for ( int shiftL = 0; shiftL < count-1; shiftL++ )
        {
            items[shiftL] = items[shiftL+1]; // shift left from the front
        }    
        items[count-1].n = *intFront;
        nRotations--; // decrement=0 will indicate no more rotations left
    }
}

Whats happening is that the first value or "head" or "front" of the array is put into a varaible 'intFront'. 发生的是将数组的第一个值或“ head”或“ front”放入可变的“ intFront”中。 I rotate everything left by the given number of rotations, hoping to just make a simple transfer at the end. 我旋转给定旋转次数剩下的所有内容,希望最后做一个简单的转移。 Guess not.. 可能不会..

  1. You overrun your array: read of items[shiftL+1] goes beyond array bounds at last iteration, 您超出了数组的范围:在最后一次迭代中,对items[shiftL+1]读取超出了数组的范围,
  2. You save a pointer to member of a structure into intFront and then override these structures by value in the inner loop - that'll sure change value intFront points to, 您将指向结构成员的指针保存到intFront ,然后在内部循环中按值覆盖这些结构-这将确保将intFront指向的值intFront
  3. There's no need for doing multiple copies, ie no need for two embedded loops, since you know by how much you need to shift ( nRotations ). 无需进行多个复制,即不需要两个嵌入式循环,因为您知道需要移位多少( nRotations )。

You're storing a pointer to a memory address, not the value itself. 您存储的是指向内存地址的指针,而不是值本身。 Thus when you shift things around in the array, what used to be in that memory address is overwritten, but the memory address is still constant. 因此,当您在数组中移动数据时,该内存地址中的内容将被覆盖,但内存地址仍保持不变。 What you really want to be doing is storing the value of items[top+1].n (w/o the & in front) and then reassign it (without the * for dereferencing). 您真正想做的是存储items [top + 1] .n(不带&开头)的值,然后重新分配它(不带*表示取消引用)。

void stack::rotate(int nRotations)
{
    for (; nRotations > 0 ;) // Number of Rotations to the left
    {
            intFront = items[top+1].n; 
        for ( int shiftL = 0; shiftL < count-2; shiftL++ )
        {
            items[shiftL] = items[shiftL+1]; // shift left from the front
            }    
        items[count-1].n = intFront;
        nRotations--; // decrement=0 will indicate no more rotations left
    }
}

Nikolai's tips are good as well - you don't really need to do this in two nested loops (which is O(N^2) ); Nikolai的技巧也很好-您实际上不需要在两个嵌套循环( O(N ^ 2) )中执行此操作; you could do it with just a single loop (or two sequential loops for simplicity) which would be O(N) time. 您可以只用一个循环(或为简单起见两个连续循环)来完成,这将是O(N)时间。

You might consider using a valarray , it would save you from manually having to implement a shift/rotate operation on the array. 您可能会考虑使用valarray ,这将使您免于手动执行数组上的shift / rotate操作的麻烦 See member functions rotate/crotate. 请参阅成员函数旋转/ cro缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM