什么是创建在C ++中已有的阵列子阵的最佳方式? [英] What is the best way to create a sub array from an exisiting array in C++?

查看:144
本文介绍了什么是创建在C ++中已有的阵列子阵的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我想从现有阵列子阵列,我只是不知道如何去做。在我的例子我有一个非常大的数组,但我想创建从数组的最后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,并把它们放到数组2,但我不想要复制的阵列。我只想ARRAY2在96元开始,这样数组1 [96]和数组2 [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 ++中已有的阵列子阵的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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