在C ++中数组的大小动态? [英] Dynamic size of array in c++?

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

问题描述

我很困惑。我不知道我应该用什么容器。我告诉你,我需要什么第一。基本上我需要能够存储x对象的数目的容器(和对象的数量是未知的,也可能是1 - 5万)。

I am confused. I don't know what containers should I use. I tell you what I need first. Basically I need a container that can stored X number of Object (and the number of objects is unknown, it could be 1 - 50k).

我读了很多,在这里阵VS列表它说:阵列需调整大小,如果对象的数量是未知的(我不知道如何在C ++中调整数组大小),并且还指出,如果使用链表,如果你要搜索某个项目,它将通过(迭代)从第一循环结束(或者相反),而一个数组可以在索引数组对象中指定。

I read a lot, over here array vs list its says: array need to be resized if the number of objects is unknown (I am not sure how to resize an array in C++), and it also stated that if using a linked list, if you want to search certain item, it will loop through (iterate) from first to end (or vice versa) while an array can specify "array object at index".

然后我去一个其他的解决办法,地图,矢量等。像这样的:的阵VS矢量。有些应答说从来不使用数组。

Then I went for an other solution, map, vector, etc. Like this one: array vs vector. Some responder says never use array.

我是新的C ++,我只使用过数组,向量,列表和地图。现在,对于我来说,什么样的容器,你会建议我用?让我改一下我的要求:

I am new to C++, I only used array, vector, list and map before. Now, for my case, what kind of container you will recommend me to use? Let me rephrase my requirements:


  • 需要一个容器

  • 存储对象的数量是未知的,但巨大(1 - 40K也许)

  • 我需要通过容器循环来找到特定对象

推荐答案

的std ::矢量 是你所需要的。结果
你必须选择一个STL容器时要考虑两件事情。

std::vector is what you need.
You have to consider 2 things when selecting a stl container.


  1. 数据要存储

  2. 操作要对存储的数据进行

在这里SO一个问题,这depitcs这有WASA好图,我找不到链接,但我有它保存前很长一段时间,那就是:

There wasa good diagram in a question here on SO, which depitcs this, I cannot find the link to it but I had it saved long time ago, here it is:

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

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