数组转移到下一个元素

时间:2021-11-20 21:31:00

How can I move elements in an array to the next element

如何将数组中的元素移动到下一个元素

eg: x[5] = { 5, 4, 3, 2, 1 }; // initial values
    x[0] = 6; // new values to be shifted
    x[5] = { 6, 5, 4, 3, 2 }; // shifted array, it need to be shifted, 
                              // not just increment the values.

This what I've done so far. It's wrong, that's why I need help here. Thanks in advance.

这就是我到目前为止所做的。这是错的,这就是我需要帮助的原因。提前致谢。

#include <iostream>

using namespace std;

int main() 
{
  int x[5] = { 5, 4, 3, 2, 1 };

  int array_size = sizeof(x) / sizeof(x[0]);

  x[0] = 6;

  int m = 1;

  for(int j = 0; j < array_size; j++) {
    x[m+j] = x[j];
    cout << x[j] << endl;
  }

  return 0;
}

7 个解决方案

#1


8  

#include <iostream>

int main () {

  int x[5] = { 5, 4, 3, 2, 1 };

  int array_size = sizeof (x) / sizeof (x[0]);

  for (int j = array_size - 1; j > 0; j--) {

      x[j] = x[j - 1];
  }

  x[0] = 6;

  for (int j = 0; j < array_size; j++) {

      std::cout << x[j];
  }

  return 0;
}

#2


14  

#include<algorithm>

// ...
std::rotate(x, x+4, x+5);
x[0] = 6;

#3


8  

To "move rightwards" you have to iterate from the end of array:

要“向右移动”,你必须从数组的末尾迭代:

for(int j = array_size - 2; j >= 0; j--) {
   x[m+j] = x[j];
   cout << x[j] << endl;
}   

otherwise you just overwrite all the elements with the 0th element.

否则你只需用第0个元素覆盖所有元素。

Please note array_size - 2 - otherwise you have "off by one" trying to access the element beyond the array end and that's undefined behavior.

请注意array_size - 2 - 否则你有“off by one”试图访问数组末尾之外的元素,这是未定义的行为。

#4


4  

First of all, you should shift the old values in the array before you write the new value. But instead of a loop, you are better of using memmove(). Or even better with std::vector instead of an array - it handles all these low-level issues for you, including automatically resizing the array when needed.

首先,在写入新值之前,应该在数组中移动旧值。但不是循环,你最好使用memmove()。或者甚至更好地使用std :: vector而不是数组 - 它会为您处理所有这些低级问题,包括在需要时自动调整数组大小。

#5


1  

In the general case where you need to shift m elements (where 0 <= m <n): Start from the end of the array. If you start at the begining (index 0) then you overwrite and then move that overridden value.

在一般情况下,你需要移动m个元素(其中0 <= m ):从数组的末尾开始。如果从开头(索引0)开始,则覆盖然后移动该重写值。

Studying the source code of std::memmove may be instructive as well.

研究std :: memmove的源代码也可能具有指导意义。

#6


0  

You can start from the end of the array. You copy the

您可以从数组的末尾开始。你复制了

  • element in 2nd last position to the last position,
  • 第二个位置到最后一个位置的元素,
  • element in 3rd last position to the 2nd last position,
  • 元素在第3个最后位置到第2个最后位置,
  • ....
  • ....
  • element in first position(index 0) to the 2nd position and finally
  • 元素在第一个位置(索引0)到第二个位置,最后
  • copy the new number in the first position. .
  • 将新号码复制到第一个位置。 。

.

for(j = array_size-1; j >0; j--) {
 x[j] = x[j-1];
}
x[0] = 6;

#7


0  

    #include <iostream>

    using namespace std;

    int main() 
    {
       int x[5] = { 5, 4, 3, 2, 1 };

        int array_size = sizeof(x) / sizeof(x[0]);

        int m = 1;

        for(int j = array_size-1; j > 0; j--) {
           x[j] = x[j-m];
           cout << x[j] << endl;
        }

       x[0] = 6;
       return 0;
    }

#1


8  

#include <iostream>

int main () {

  int x[5] = { 5, 4, 3, 2, 1 };

  int array_size = sizeof (x) / sizeof (x[0]);

  for (int j = array_size - 1; j > 0; j--) {

      x[j] = x[j - 1];
  }

  x[0] = 6;

  for (int j = 0; j < array_size; j++) {

      std::cout << x[j];
  }

  return 0;
}

#2


14  

#include<algorithm>

// ...
std::rotate(x, x+4, x+5);
x[0] = 6;

#3


8  

To "move rightwards" you have to iterate from the end of array:

要“向右移动”,你必须从数组的末尾迭代:

for(int j = array_size - 2; j >= 0; j--) {
   x[m+j] = x[j];
   cout << x[j] << endl;
}   

otherwise you just overwrite all the elements with the 0th element.

否则你只需用第0个元素覆盖所有元素。

Please note array_size - 2 - otherwise you have "off by one" trying to access the element beyond the array end and that's undefined behavior.

请注意array_size - 2 - 否则你有“off by one”试图访问数组末尾之外的元素,这是未定义的行为。

#4


4  

First of all, you should shift the old values in the array before you write the new value. But instead of a loop, you are better of using memmove(). Or even better with std::vector instead of an array - it handles all these low-level issues for you, including automatically resizing the array when needed.

首先,在写入新值之前,应该在数组中移动旧值。但不是循环,你最好使用memmove()。或者甚至更好地使用std :: vector而不是数组 - 它会为您处理所有这些低级问题,包括在需要时自动调整数组大小。

#5


1  

In the general case where you need to shift m elements (where 0 <= m <n): Start from the end of the array. If you start at the begining (index 0) then you overwrite and then move that overridden value.

在一般情况下,你需要移动m个元素(其中0 <= m ):从数组的末尾开始。如果从开头(索引0)开始,则覆盖然后移动该重写值。

Studying the source code of std::memmove may be instructive as well.

研究std :: memmove的源代码也可能具有指导意义。

#6


0  

You can start from the end of the array. You copy the

您可以从数组的末尾开始。你复制了

  • element in 2nd last position to the last position,
  • 第二个位置到最后一个位置的元素,
  • element in 3rd last position to the 2nd last position,
  • 元素在第3个最后位置到第2个最后位置,
  • ....
  • ....
  • element in first position(index 0) to the 2nd position and finally
  • 元素在第一个位置(索引0)到第二个位置,最后
  • copy the new number in the first position. .
  • 将新号码复制到第一个位置。 。

.

for(j = array_size-1; j >0; j--) {
 x[j] = x[j-1];
}
x[0] = 6;

#7


0  

    #include <iostream>

    using namespace std;

    int main() 
    {
       int x[5] = { 5, 4, 3, 2, 1 };

        int array_size = sizeof(x) / sizeof(x[0]);

        int m = 1;

        for(int j = array_size-1; j > 0; j--) {
           x[j] = x[j-m];
           cout << x[j] << endl;
        }

       x[0] = 6;
       return 0;
    }