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

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

问题描述

我很困惑.我不知道我应该使用什么容器.我先告诉你我需要什么.基本上我需要一个可以存储X个对象的容器(对象的数量是未知的,可能是1-50k).

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

我读了很多,在这里 数组与列表 它说:数组需要如果对象的数量未知,则调整大小(我不确定如何在 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".

然后我选择了其他解决方案,地图,矢量等.就像这个:数组与向量.一些响应者说永远不要使用数组.

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::vector 是您所需要的.
选择 stl 容器时必须考虑两件事.

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

  1. 您要存储的数据
  2. 您要对存储的数据执行的操作

在 SO 上的一个问题中有一个很好的图表,它描述了这个,我找不到它的链接,但我很久以前就保存了它,这里是:

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