问题描述
好的,我正在尝试从现有数组中获取子数组,但我不知道该怎么做.在我的示例中,我有一个非常大的数组,但我想从数组的最后 5 个元素创建一个数组.
OK, I am trying to get a sub array from an existing array and I'm just not sure how to do it. In my example I have a very large array, but I want to create an array from the last 5 elements of the array.
我正在谈论的一个例子是:
An example of what I am talking about would be:
int array1 = {1,2,3,...99,100};
int array2[5] = array1+95;
我知道这不正确,但我在正确处理时遇到了一些麻烦.我想在 array1 中获取元素 96 到 100 并将它们放入 array2 但我不想复制数组.我只希望 array2 从 96 元素开始,这样 array1[96] 和 array2[0] 将指向相同的位置.
I know this isn't correct, but I am having some trouble getting it right. I want to get the elements 96 through 100 in array1 and put them into array2 but I don't want to copy the arrays. I just want array2 to start at the 96 element such that array1[96] and array2[0] would be pointing to the same location.
推荐答案
为此:
"such that array1[96] and array2[0] would be pointing to the same location."
你可以:
int *arr2 = arr1 + 96;
assert(arr2[0] == arr1[96] == 97);
这篇关于从 C++ 中的现有数组创建子数组的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!