如何制作具有动态大小的数组?动态数组的一般用法(也可能是指针)? [英] How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

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

问题描述

我正在尝试制作一个程序

I'm trying to make a program which

  1. 接受用户输入(假设所有输入都是 int)
  2. 将其存储在没有起始大小的数组中(即不是 -> array[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.

我知道这是一个非常菜鸟的问题,是的,我是菜鸟,但要改变这一点,我需要一些帮助.

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

推荐答案

对于C++:

如果您只需要一个容器,只需使用 std:vector.它将处理您所需的所有内存分配.但是,如果您想开发自己的动态容器(无论出于何种原因),您都必须自己处理内存分配.也就是说,当您的数组增长时,您必须分配新的内存块,将当前数组值复制到新的内存位置,并将新值添加到新分配的内存中.通常将这种逻辑包装在一个单独的类中,例如GrowingArray(如标准提供的vector 类)

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):

将它存储在一个没有起始大小的数组中(即不是 -> array[5];)

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

这里你想使用这样的东西:int * myDynamicArray;当用户输入一些值时,您会分配内存块以存储这些值:myDynamicArray = new int[5]; 与初始输入的大小.我也建议将数组的大小保存在某个变量中:int arraySize = 5;如果稍后您想将新值附加到 myDynamicArray,首先您必须为增长的数组(当前数组元素 + 新数组元素)分配新的内存块.假设您有 10 个新值.然后你会这样做: int* growArray = new int[arraySize+10]; 这会为增长的数组分配新的内存块.然后,您想将旧内存块中的项目复制到新内存块并添加用户附加值(我认为您将其用于学习目的,因此我为您提供了用于复制元素的简单循环.您可以使用 std:copy 或 c 像 memcopy 一样):

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天全站免登陆