C++ 数组移动到下一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3650372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array shifting to the next element
提问by Azam
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;
}
回答by wilhelmtell
#include<algorithm>
// ...
std::rotate(x, x+4, x+5);
x[0] = 6;
回答by Cedric H.
#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;
}
回答by sharptooth
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
- 否则您将“逐一”尝试访问数组末尾之外的元素,这是未定义的行为。
回答by Péter T?r?k
First of all, you should shift the old values in the array beforeyou 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
而不是数组 - 它为您处理所有这些低级问题,包括在需要时自动调整数组的大小。
回答by dirkgently
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
元素的一般情况下( where 0 <= m <n
):从数组的末尾开始。如果从开头(索引 0)开始,则覆盖并移动该覆盖值。
Studying the source code of std::memmove
may be instructive as well.
研究 的源代码std::memmove
也可能是有益的。
回答by codaddict
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,
- ....
- element in first position(index 0) to the 2nd position and finally
- copy the new number in the first position. .
- 从倒数第二个位置到最后一个位置的元素,
- 倒数第三个位置到倒数第二个位置的元素,
- ....
- 第一个位置(索引 0)的元素到第二个位置,最后
- 复制第一个位置的新数字。.
.
.
for(j = array_size-1; j >0; j--) {
x[j] = x[j-1];
}
x[0] = 6;
回答by Anil Vishnoi
#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;
}