c ++/CLI移位数组 [英] c++/CLI Shifting an array

查看:97
本文介绍了c ++/CLI移位数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何移动数组.

说我有一个数组int data=[20];

并想向右移动,因此数据[19]消失了,而18移到了19.然后我用一个新数字填充[0].

我需要使用类似
的东西吗

I am trying to figure out how to shift an array.

Say I have an array int data=[20];

and wanted to shift right so data [19] is gone and 18 moves to 19. Then I would fill [0] with a new number.

Would I have to use Something like

for (i=19, i<=0, i--)
{
    data[i]=data[i-1]
}




还是我会使用像记忆之类的东西?

感谢您的帮助




or would I use something like memmove?

Thanks for the help

推荐答案

[支持@OriginalGriff答案:]

使用memmove可以提高性能,但会牺牲可读性并有机会使自己陷入困境.请记住,您正在处理原始的内存位置,并且可能会有轻微的变化....您知道我的意思.

如果要处理少量数据,请坚持使用for循环.它可读性强,不难编写,易于调试和维护,但与记忆体相比,它的性能受到了影响.

如果您要处理大量数据,则memmove可能是您的朋友,但请谨慎处理.请记住,一旦移动数据,仍然需要覆盖第一个索引(在这种情况下为索引0).

顺便说一句:正如我指出的那样,@ originalgriff的代码需要一些改进,例如

[In support of @OriginalGriff Answer:]

using memmove you will gain performance but at an expense of readability and a chance to shoot yourself in the foot. Remember you are dealing with raw mem locations and slight shift can have.... you know what I mean.

If you are dealing with small set of data, then stick with for loop. It is readable, not hard to write and easy to debug and maintain, but it will have performance hit as compared to memmove.

If you are dealing with very large set of data, then memmove may be your friend, but handle with care. Remember you still need to over-write your first index (index 0 in this case) once you move your data.

BTW: as I pointed out, @originalgriff''s code needs some improvements, like this

const int SIZE = 20;
memmove(data+1, data, (sizeof(int)*(SIZE-1)));


我想我会给您一些可以使用的解决方案.
有趣的是-没有人注意到即使对于``for''循环示例的伪代码也是无效的.
Just thought I would give you a number of solutions that you can play with.
Interesting - no one noted that even as pseudo code that ''for'' loop example is invalid.
#include <algorithm> // if C++ copy_backward() function template is used
#include <cstring>   // if C memmove function is used

typedef int T; // defined to generalize the solutions

// Solution 1: If pointer to memory holding 20 elements of type T
//   Exmple: T* data = new T[array_size];
const size_t array_size = 20;
std::copy_backward (data, data+array_size-1, data+array_size);
data[0] = 0;

// Solution 2: If an array of elements of type T
//   Exmple: T data[20];
const size_t array_size = sizeof(data)/sizeof(data[0]);
std::copy_backward (data, data+array_size-1, data+array_size);
data[0] = 0;

// Solution 3: Alternate solution to solutions 1 & 2
for( size_t i = array_size-1; i > 0; --i )
{
	data[i] = data[i-1]; // OR *(data+i) = *(data + (i-1));
}
data[0] = 0;

// Solution 4: Alternate solution to 3 - should be faster than 3
size_t i = array_size;
while( --i > 0 )
{
	data[i] = data[i-1]; // OR *(data+i) = *(data + (i-1));
}
data[0] = 0;

// Solution 5: Same as calling copy_backward() in solution 1 & 2
T* first = data + 1;
T* last = data + array_size - 1;
T* dest = data + array_size;
while( last != first )
{
	*(--dest) = *(--last);
}
data[0] = 0;

// Solution 6: If type T is a POD - built-in type or C-style structure
// Notes:
//   1. memmove() is normally implemented in assembly - should be fastest method
//   2. STL implementors often create specialized (optimized) versions of their
//      algorithms that call memmove() for built-in types like char, int, etc.
memmove(data+1, data, sizeof(data)-sizeof(data[0]));
data[0] = 0;


<pre lang="cs">int newNum;<br />
for (int i=19;i>0;i--)<br />
    {<br />
        a[i]=a[i-1];<br />
    }<br />
    a[0]=nuwNum;</pre><br />


这篇关于c ++/CLI移位数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆