向量与数组是否包含大量元素? [英] Vector vs Array for large number of elements?

查看:85
本文介绍了向量与数组是否包含大量元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,带有大约1000个值(准确地说是字符串),每个值都附加有一个键.键不一定是唯一的(即,同一键可以有5个值).我正在创建一个程序,将这些数据组织到一棵树中,使得树的每个节点都由它的键来组织,并在该节点中存储与该键关联的列表.

I have a text file with about 1000 values (strings, to be precise), each of which has a key attached to it. The keys are not necessarily unique (that is, there can be 5 values with the same key). I am creating a program that organizes this data into a tree such that each node of the tree is organized by it's key, storing a list of that key's associated in the node.

我不知道每个键可以有多少个值-大概是1到5.

I do not know how many values per key I can expect - it would probably be from 1 to 5, thereabouts.

此外,一旦存储了值,我将不需要对其进行任何更改.我只需要阅读它们.

Also, once the values are stored, I will not need to perform any changes on them. I will only need to read them.

我的问题是这样:

从内存使用的角度来看,使用向量或数组(大小为5)存储值是否更有意义?

From a memory-usage standpoint, would it make more sense to use vectors or arrays (of size 5) to store the values?

使用向量意味着我只需要存储所需的数量即可.但是,使用数组将意味着我不必使用向量对象.我不确定哪个会更有效.

Using the vectors would mean that I would only need to store as many as I needed. However using an array would mean I don't have to use the vector object. I'm not sure which one would be more efficient.

非常感谢您的宝贵时间.

Thank you very much for your time.

推荐答案

考虑C ++ 11的std::array,对于您的特定情况,它更快,比std::vector小且比array[]更安全.

Consider C++11's std::array, it is faster, smaller than std::vector and safer than array[] for your particular case.

这篇关于向量与数组是否包含大量元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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