显示动态数组中的数字 [英] Displaying a number from dynamic array

查看:71
本文介绍了显示动态数组中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我遇到了问题。让我们说,我有一个充满数据(数字)的文件,我想把它们读成动态数组。在做完之后,我想看看,例如,数组的第三个元素和屏幕中的全部数字。我怎么能这样做?

这里是代码

展开 | 选择 | Wrap | 行号

解决方案

你考虑过使用矢量吗?它将为您处理大小和动态分配。


Adrian



你考虑过使用矢量吗?它将为您处理大小和动态分配。


Adrian



谢谢你。现在我正在使用向量。


代码中的问题是你只输入*数组或数组的第一个元素。每次文件中有数字时,都会覆盖第一个元素。现在,第三个元素(array [2])从未被初始化并仍然保持garbarge值 - 就像你得到的那样。您应该使用size作为数组的索引:inf>>阵列[大小++]

Hi everyone,

I'' ve got a problem. Let''s say, I''ve got a file full of data (numbers) and I want to read them into a dynamic array. After doing it, I want to see, for instance, the third element of the array and the whole amount of numbers in the screen. How could I do that?
Here''s the code

Expand|Select|Wrap|Line Numbers

解决方案

Have you considered using a vector? It will handle the size and dynamic allocation for you.


Adrian


Have you considered using a vector? It will handle the size and dynamic allocation for you.


Adrian

Thanks man. Now I''m working with vectors.


The problem in your code is that you are only inputting into *array, or the first element of the array. This first element is overwritten every time there is a number in your file. Now, the third element (array[2]) has never been initialized and still holds garbarge values - like the one you got. You should use size as the index of the array: inf >> array[size++]


这篇关于显示动态数组中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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