如何使动态大小的数组?动态数组的一般用法(也许太指针)? [英] How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

查看:188
本文介绍了如何使动态大小的数组?动态数组的一般用法(也许太指针)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个程序,


  1. 将用户输入(假设这一切是 INT

  2. 储存于一个数组没有一个起始大小(即,不是 - > 数组[5]; );然后

  3. 使用存储阵列的任何不可告人的目的的信息。

我寻求帮助,这样我可以学习如何在需要做到这一点对我自己的。

I'm asking for help so that I can learn how to do this on my own if needed.


  • 我没有做一组大小动态数组怎么办?

  • 如何使用/门禁/上述阵列中达到的元素?

阅读只是没有解释不够我。

Reading just didn't explain enough for me.

我知道这是一个非常noobish的问题,是的,我是小白,但要改变,我需要一些帮助。

I know it's a very noobish question, and yes, I am a noob, but to change that I need some help.

推荐答案

对于 C ++

如果你只需要一个集装箱只需要使用 STD:向量。它会照顾所有的内存分配必要的为您服务。但是,如果你想开发自己的动态容器(你有什么原因)你要照顾了内存分配自己。也就是说,当你的数组增长,你必须分配新的内存块,present数组值复制到新的存储位置,然后添加新值到新分配的内存。通常一个包这样的逻辑一个单独的类内如 GrowingArray (如提供的标准矢量类)

If you just need a container just use std:vector. It will take care all the memory allocations necessary for you. However if you want to develop your own dynamic container (whatever reasons you have) you have to take care off the memory allocations yourself. That is, when your array grows you have to allocate new memory chunk, copy present array values to the new memory location and add new values to the newly allocated memory. Usually one wraps this kind of logic inside a separate class e.g. GrowingArray(like standard provided vector class)

修改

要更多地讨论我的答案(因为你正在使用这个学习目的):

To elaborate more on my answer (given that you are using this for learning purpose):

其存放在数组没有起始大小(即,不是 - >阵[5];)

store it in an array without a starting size (i.e. not -> array[5];)

下面要使用这样的事:为int * myDynamicArray;
当用户输入一些值,你分配内存块,其中这些值将会存储: myDynamicArray = INT新[5]; 与初始输入的大小。我也建议您保存某些变量数组的大小: INT ARRAYSIZE = 5;
如果以后要新的价值附加到 myDynamicArray 首先,你必须分配给成年阵列(当前数组元素+新数组元素)新的内存块。比方说你有未来10新值。然后,你会怎么做:为int * grownArray =新INT [ARRAYSIZE + 10]; 这对于分配成年数组新的内存块。然后,你想从旧的内存块项目复制到新的内存块,并添加用户的附加价值(我拿因此,我只要你简单的循环复制elemts您使用此学习的目的,你可以使用 STD:复制或c如存储器复制以及):

Here you want to use something like this: int * myDynamicArray; When a user inputs some values you allocate memory chunk where those values are going to be stored: myDynamicArray = new int[5]; with the size of your initial input. I would as well recommend to save the size of the array in some variable: int arraySize = 5; If later on you want to append new values to your myDynamicArray first of all you have to allocate new memory chunk for grown array (current array elements + new array elements). Lets say you have 10 new values coming. Then you would do: int* grownArray = new int[arraySize+10]; this allocates new memory chunk for grown array. Then you want to copy items from old memory chunk to the new memory chunk and add user appended values (I take it you are using this for learning purposes thus I provided you simple for cycle for copying elemts. You could use std:copy or c like memcopy as well):

int i = 0;
for (; i < arraySize; ++i)
   {
   grownArray[i] = myDynamicArray [i];
   }
// enlarge newly allocated array:
arraySize+= 10;
for (; i < arraySize; ++i)
   {
   grownArray[i] = newValues from somewhere
   }
// release old memory
delete[] myDynamicArray;
// reassign myDynamicArray pointer to point to expanded array
myDynamicArray = gronwArray;

这篇关于如何使动态大小的数组?动态数组的一般用法(也许太指针)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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